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

后端 未结 3 2066
忘了有多久
忘了有多久 2020-12-31 06:38

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 h

3条回答
  •  隐瞒了意图╮
    2020-12-31 06:59

    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.

提交回复
热议问题