Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore .DS_Store and Icon files in a folder with Cocoa NSFIleManager

I'm trying to remove specific files from a directory using NSFileManager. I would like to ignore the hidden .DS_Store and Icon files (the folder that I'm checking has to have a custom icon) that are in the directory, however I keep accidentally deleting them as well. Right now, I'm doing the following:

 NSFileManager *manager = [NSFileManager defaultManager];
 NSArray *dirContents = [manager contentsOfDirectoryAtPath:[selectedFolder stringValue] error:nil]; 
 for (int i = 0; i < [dirContents count]; i++)
 {
     NSString *theFile = [dirContents objectAtIndex:i];

     if([theFile isEqualToString:@".DS_Store"] || [theFile isEqualToString:@"Icon?"] || [theFile isEqualToString:@"Icon"])
     { 
        continue;
     }
     //do manipulations on files here
 }
[manager release];

However, the .DS_Store and Icon files aren't being matched in my if statement. Additionally, when I show hidden files in Finder, the icon file is called "Icon". However, doing an ls in that directory in terminal prints out "Icon?".

How can properly I parse these files out in my code?

Thanks

EDIT: So it actually is successfully ignoring the .DS_Store file, but the Icon file is still getting past the if statement.

like image 875
minimalpop Avatar asked May 01 '11 20:05

minimalpop


People also ask

Can I delete .DS files?

. DS_Store files are only used by the Finder to hold custom view settings for that particular folder. In most cases nothing will happen if you delete them, or at worst custom Finder view settings (icon size, position, background color, etc) will be lost.

Can I delete DS_Store files on Windows?

Open Finder > Applications > Utilities > Terminal. E.g., To delete all DS_Store files on the desktop, enter cd desktop and press Enter. To delete all DS_Store files on other folders, type cd followed by a space, drag the folder icon to Terminal, and press Enter. Select OK.


1 Answers

Interestingly, I believe that the question part of another question posted recently essentially answers yours. If you use:

-[NSFileManager contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error:] 

(doc link), you can pass an option, NSDirectoryEnumerationSkipsHiddenFiles, to ignore hidden files so that you don't have to check for specific ones:

NSURL * selectedFolderURL = [NSURL fileURLWithPath:[selectedFolder stringValue]];
[myFileManager contentsOfDirectoryAtURL:selectedFolderURL
             includingPropertiesForKeys:[NSArray arrayWithObject:NSURLNameKey]
                                options:NSDirectoryEnumerationSkipsHiddenFiles
                                  error:&error];

Note that this returns absolute URLs, whereas the method in your question returns paths that are relative to the original directory. Easily worked around, but important to know especially if you're deleting stuff.

like image 72
jscs Avatar answered Nov 16 '22 02:11

jscs