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

女生的网名这么多〃 提交于 2019-11-30 08:35:28
Josh Caswell

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.

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];
//

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];
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!