views:

601

answers:

3
  1. What is the best way to remove a folder, deleting all subfolders and files?
  2. What is the best way to remove files with a specific extension; e.g., if I want to remove only files with '.txt' extension?

Cocoa or carbon.

+3  A: 

To remove a directory tree (or file), use -[NSFileManager removeItemAtPath:error:]. This deletes the files directly (and it will delete all of them); if you want to move the item to the Trash instead, use NSWorkspaceRecycleOperation .

As for removing only files with specific extensions: Get each pathname's pathExtension and use caseInsensitiveCompare: to compare it to the ones you're looking for, then remove the file if it's on your hit list.

If you want to combine the two (i.e., remove only files within a directory tree that have a given extension), you'll need to get a directory enumerator from the NSFileManager and walk the directory tree yourself, removing files one by one.

Peter Hosey
Unless its an internal application file it would probably be better to use the recycle operation (:
Jacob
+1  A: 

Yep, be sure to use therecycle bin unless of course they are files the user shouldn't see/know about.

What's a "recycle bin"? Question is about Cocoa so you need to start thinking "Trash"!!
Meltemi
A: 

I have a problem in file deletion. Each time I use

[fileMgr removeItemAtPath: fileNamePath error:nil];

to delete, say for example word.app, it doesn't delete the bundle but rather only the contents. So after attempting to delete the file, I am left with the word icon with a void symbol above it (round icon with a slash often used in traffic signs) which is empty.What I need is to also remove Word.app.

Need help please.

Thanks, Benson