JSA986
JSA986

Reputation: 5936

Get contents of a specific folder in documents diectory

Im trying to read contents of a specific folder in documents directory thats contains only png's /Documents/ApplianceImagesFolder/

Currently I can only get all my images from documents folder, how can I target contents of ApplianceImagesFolder only?

 //gets all png form documents folder
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
    fileList=[[NSMutableArray alloc]init];
    for (NSString *dir in dirContents) {
        if ([dir hasSuffix:@".png"]) {
            NSRange range = [dir rangeOfString:@"."];
            NSString *name = [dir substringToIndex:range.location];
            if (![name isEqualToString:@""]) {

                [fileList addObject:name];
            }
        }
    } 
    NSLog(@"document folder content list %@ ",fileList);

Upvotes: 0

Views: 92

Answers (1)

rmaddy
rmaddy

Reputation: 318774

Build the desired path:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"ApplianceImagesFolder"];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:nil];

And there's a better way to find the png files:

fileList=[[NSMutableArray alloc]init];
for (NSString *filename in dirContents) {
    NSString *fileExt = [filename pathExtension];
    if ([fileExt isEqualToString:@"png"]) {
        [fileList addObject:filename];
    }
} 
NSLog(@"document folder content list %@ ",fileList);

Upvotes: 1

Related Questions