Search code examples
iphoneiosnsarraynsbundlefile-manager

Load all files in the app bundle containing a string in their filename into a NSMutableArray?


So I have a folder called "content" in my app's bundle . I would need to load all files, which contain a string, I provide (for example "dog"), in their filename into a NSMutableArray. How can I do this, if it even is possible?

Thanks a lot in advance!


Solution

  • NSMutableArray *resultFiles = [NSMutableArray array];
    NSFileManager *fm = [[NSFileManager alloc] init];
    NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
    NSError *error = nil;
    NSArray *files = [fm contentsOfDirectoryAtPath:bundlePath error:&error];
    [fm release];
    
    if (!error) {
        for (NSString *filename in files) {
            NSRange range = [filename rangeOfString:@"dog"];
            if (!(range.location == NSNotFound && range.length == 0)) {
                // filename contains "dog"
                [resultFiles addObject:filename];
            }
        }
    }