21

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?

codeetcetera
  • 3,213
  • 4
  • 37
  • 62
  • Your title says to get all resources paths, but the body says to only get txt so yeah a bit two sided. :-/ I came here looking for a way to get all files. – Jonny Aug 23 '13 at 01:27
  • You should be able to do the same thing with the NSDirectoryEnumerator below, just remove the conditional that checks for the file type. – codeetcetera Aug 23 '13 at 14:09

5 Answers5

24

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
}
Mazyod
  • 22,319
  • 10
  • 92
  • 157
codeetcetera
  • 3,213
  • 4
  • 37
  • 62
7

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;
}
iamamused
  • 2,720
  • 3
  • 25
  • 17
4

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

rekle
  • 2,375
  • 1
  • 18
  • 9
1

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
}
ChrisJF
  • 6,822
  • 4
  • 36
  • 41
0

Before copying think about create symlink. You can create just one symlink to folder with a lot of items. And it will work in most situations.

You will be free from creating another thread, waiting for end of operation, etc.

Sergey Kopanev
  • 1,496
  • 3
  • 17
  • 29