15

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.

minimalpop
  • 6,997
  • 13
  • 68
  • 80
  • 2
    If you write `NSLog(@"%@", theFile);` right after setting `theFile`, do those icon file names show up exactly like you’re testing in that `if`? –  May 01 '11 at 22:00
  • +1 Bavarious. It's a good bet that the `?` that you see in Terminal is not actually an ASCII `?` – jscs May 01 '11 at 22:17

3 Answers3

27

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.

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
  • This method returns file URLs instead of file paths, so it requires additional modifications to the code. – Nicolas Miari Jan 28 '15 at 03:09
  • 1
    `[theURL path]` seemed a little too obvious to have to mention. – jscs Jan 29 '15 at 00:22
  • I know ;) In any case, using URLs instead of paths seems to be the more modern, recommended way these days. – Nicolas Miari Jan 29 '15 at 00:26
  • Quite right in my opinion; the path-based API is more limited in certain places. – jscs Jan 29 '15 at 00:32
  • URLs are definitely more powerful. But what I wanted to remark is, beside the change from NSString to NSURL, the old method returns an array of filenames relative to the directory path; the new one returns full (absolute) URLs. That bit me until I found out. – Nicolas Miari Jan 29 '15 at 00:37
3

The filename of a folder's custom icon resource is "Icon\r" (Icon, followed by a carriage return).

What I generally do when enumerating a directory in which I want to skip invisible items (those whose name starts with a "."), is to check for a prefix of @".":

NSMutableArray *fullPaths = [NSMutableArray array];

NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];

NSArray *subpaths = [fileManager subpathsAtPath:filePath];

for (NSString *subpath in subpaths) {
   if ( ![[subpath lastPathComponent] hasPrefix:@"."] && 
        ![[subpath lastPathComponent] isEqualToString:@"Icon\r"]) {
        [fullPaths addObject:[filePath stringByAppendingPathComponent:subpath]];
   }
}
// continue

The above code will work in 10.5 and later, (or even 10.0, I believe, if you changed the fast enumeration to use an NSEnumerator).

P.S. If you are creating your NSFileManager using +defaultManager, then you shouldn't use the [manager release] line, as that would be over-releasing.

So, instead of:

NSFileManager *manager = [NSFileManager defaultManager];
// 
[manager release];

do

NSFileManager *manager = [[NSFileManager alloc] init];
//
[manager release];

or

NSFileManager *manager = [NSFileManager defaultManager];
//
NSGod
  • 22,699
  • 3
  • 58
  • 66
  • 1
    This is unnecessary and possibly dangerous for what the OP was asking. First, `subPathsAtPath:` (whose usage is [not recommended after 10.4](http://goo.gl/RPNfN) (see "Special Considerations")) recurses on subdirectories, which means this may yield files that are _not_ in the original directory. Moreover, `NSFileManager` will skip invisible files _for_ you with `NSDirectoryEnumerationSkipsHiddenFiles`, and can give you an enumerator over the directory contents with `enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:` if that's what you want (although, N.B., that also recurses). – jscs May 02 '11 at 02:17
  • 1
    One remark: not every hidden file has a name that starts with `.`, e.g. `SetFile -a V /path/to/file`. –  May 02 '11 at 02:18
-1

Simple method:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (int i = 0; i < [imageFilenames count]; i++)
    {

    NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];

        if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"])
        {
          UIImage *myimage = [UIImage imageWithContentsOfFile:imageName];
          UIImageView *imageView = [[UIImageView alloc] initWithImage:_myimage];
        }
    }
Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90