Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get all resource paths in my bundle recursively in iOS?

Tags:

ios

iphone

ipad

I have an "unzipped" folder in my application bundle. I need to get the resource paths for all files of type txt. I've been using this,

NSArray *filePaths = [NSBundle pathsForResourcesOfType:@"txt" inDirectory:[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/unzipped"]];

But it only gets files in the first directory. Is there a recursive version of this somewhere that I'm just missing?

like image 598
codeetcetera Avatar asked Apr 29 '11 19:04

codeetcetera


4 Answers

I got this working using the code posted by @rekle as a starting point. The trick is to use NSDirectoryEnumerator, which will do this recursively. Here's the function I wrote in case anyone needs it.

- (NSArray *)recursivePathsForResourcesOfType:(NSString *)type inDirectory:(NSString *)directoryPath{

    NSMutableArray *filePaths = [[NSMutableArray alloc] init];

    // Enumerators are recursive
    NSDirectoryEnumerator *enumerator = [[[NSFileManager defaultManager] enumeratorAtPath:directoryPath] retain];

    NSString *filePath;

    while ((filePath = [enumerator nextObject]) != nil){

        // If we have the right type of file, add it to the list
        // Make sure to prepend the directory path
        if([[filePath pathExtension] isEqualToString:type]){
            [filePaths addObject:[directoryPath stringByAppendingPathComponent:filePath]];
        }
    }

    [enumerator release];

    return [filePaths autorelease];
}

Swift, using NSURL

func recursivePathsForResources(type type: String) -> [NSURL] {

    // Enumerators are recursive
    let enumerator = NSFileManager.defaultManager().enumeratorAtPath(bundlePath)
    var filePaths = [NSURL]()

    while let filePath = enumerator?.nextObject() as? String {

        if NSURL(fileURLWithPath: filePath).pathExtension == type {
            filePaths.append(bundleURL.URLByAppendingPathComponent(filePath))
        }
    }

    return filePaths
}
like image 177
codeetcetera Avatar answered Nov 18 '22 15:11

codeetcetera


Just an update to this method to allow any file types (nil) and provide the correct path format

- (NSArray *)recursivePathsForResourcesOfType:(NSString *)type inDirectory:(NSString *)directoryPath{

    NSMutableArray *filePaths = [[NSMutableArray alloc] init];
    NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:directoryPath];

    NSString *filePath;

    while ((filePath = [enumerator nextObject]) != nil){
        if (!type || [[filePath pathExtension] isEqualToString:type]){
            [filePaths addObject:[directoryPath stringByAppendingPathComponent:filePath]];
        }
    }

    return filePaths;
}
like image 24
iamamused Avatar answered Nov 18 '22 16:11

iamamused


Try something like this:

NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSError *error = nil;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:&error];

'files' will be an array of strings containing all the filenames in that path...

like image 4
rekle Avatar answered Nov 18 '22 17:11

rekle


Swift 3.1 version of the answer from smokinggoyster

func recursivePathsForResources(type: String, in directoryPath: String) -> [String] {
    // Enumerators are recursive
    let enumerator = FileManager.default.enumerator(atPath: directoryPath)
    var filePaths: [String] = []

    while let filePath = enumerator?.nextObject() as? String {

        if URL(fileURLWithPath: filePath).pathExtension == type {
            filePaths.append(directoryPath.byAppending(pathComponent: filePath))
        }
    }
    return filePaths
}
like image 1
ChrisJF Avatar answered Nov 18 '22 15:11

ChrisJF