Search code examples
iosnsfilemanager

.DS_Store file skipping


In my application I Use NSFileManager to get the number of files in a folder using following code

 NSFileManager *manager=[NSFileManager defaultManager];
 NSString *path;
 int numberofFiles=[[manager contentsOfDirectoryAtPath:path error:nil] count];
 numberofFiles=numberofFiles-1; //number of files except .DS_Store

But my problem is that the file .DS_Store is not always created defaultly, at that time I get less count than the count of files actually present in that directory .

So is there a method in NSFileManager which return the array of files excluding .DS_Store or I have to exclude manually using -IsEqualToString method or else is there any option to create a new directory without .DS_Store file.


Solution

  • Explicitly look for the .DS_Store file and adjust the count if it's found:

    NSFileManager *manager=[NSFileManager defaultManager];
    NSString *path = ...;       // Presumably this is a valid path?
    NSArray *contents = [manager contentsOfDirectoryAtPath:path error:nil];
    NSUInteger numberOfFiles = [contents count];
    if ([contents indexOfObject:@".DS_Store"] != NSNotFound)
        numberOfFiles--;