vignesh kumar
vignesh kumar

Reputation: 2330

.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.

Upvotes: 3

Views: 1553

Answers (2)

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Try this.. Its working for me..

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

Upvotes: 0

trojanfoe
trojanfoe

Reputation: 122391

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

Upvotes: 2

Related Questions