-2

I am reading files from folder, folder contains .pngs but I get additionally Thumb.db file in every folder and all others are .pngs. I read content as

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];

How to remove all files which contains .db in array or to stay just which ends with .png ?

Ekta Padaliya
  • 5,553
  • 2
  • 37
  • 46
PaolaJ.
  • 9,104
  • 19
  • 62
  • 100

3 Answers3

4

Idiomatic way of filtering arrays in Cocoa is with NSPredicate:

NSArray *filtered = [directoryContent filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"pathExtension = png"]];

pathExtension method mentioned inside the predicate is a method on NSString that tries to interpret the string as a filesystem path, and return the extension.

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
3

I think by using NSPredicate method you can get exact values

NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
NSString *str              = @".png"
NSPredicate *sPredicate    = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",str];
NSArray *predicateArray    = [directoryContent filteredArrayUsingPredicate:sPredicate];
0

Try this code:

    NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
    NSMutableArray *imageArray = [[NSMutableArray alloc] init];

    for (NSString *path in directoryContent) 
    {
       if ([path hasSuffix:@".png"]) 
       {
          [imageArray addObject: path];
       }
    }

    NSLog (@"Number of images : %d" , [imageArray count]);
Mrunal
  • 13,474
  • 6
  • 48
  • 91