0

I have a folder with 4 subfolders in my iOS application with each of these containing about 20 files each. I would like to be able to iterate through each folder and print out the filenames. I am not sure how to go about doing this.

Here is what I have tried:

    let docsPath = NSBundle.mainBundle().resourcePath! + "/Samples";
    let fileManager = NSFileManager.defaultManager()

    var error: NSError?
    let docsArray = fileManager.contentsOfDirectoryAtPath(docsPath, error:&error)
    println(docsArray)

This prints out nil. I expect it to print out each of the filenames. How do I make this happen?

TestinginProd
  • 1,085
  • 1
  • 15
  • 35

3 Answers3

0

You have two problems here:

1)

Check your built app to see if "Samples" is really ending up in the built binary. From the error of "The operation couldn’t be completed", I'm thinking you aren't copying "Samples" into the compiled app bundle or at least into the place you're expecting it to be.

2)

The call you're doing will give you the contents of the folder, but not the contents of the subfolders which is what you really want to list.

Use NSDirectoryEnumerator instead to get the contents of that folder and subfolders. Here is a related question that might give you one direction to go.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

You can use the NSFileManager's enumerator if you want to get all the files including inside subdirectories.

Simple example:

if let enumerator = fileManager.enumeratorAtURL(docsPath, includingPropertiesForKeys: nil, options: nil, errorHandler: nil) {
    while let url = enumerator.nextObject() as? NSURL {
        println(url)
    }
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 1
    You do not have to use the `enumeratorAtURL` function. The `contentsOfDirectoryAtPath` function is just fine. – rmaddy May 29 '15 at 23:35
  • `contentsOfDirectoryAtPath` does not traverse subdirectories, this one does; I've edited my answer with this info. – Eric Aya May 29 '15 at 23:38
  • True. It depends on what the OP needs. But since they are getting `nil`, it most likely means the path isn't valid so using `enumeratorAtURL` won't help. – rmaddy May 29 '15 at 23:39
0

Nevermind, I figured it out:

    var docsPath = NSBundle.mainBundle().resourcePath! + "/Snare";
    let fileManager = NSFileManager.defaultManager()

    var error: NSError?
    let docsArray = fileManager.contentsOfDirectoryAtPath(docsPath, error:&error)

    //println(error!.localizedDescription)
    println(docsArray)


    for filename in docsArray!
    {
        let subfolderPath = docsPath + "/"+(filename as! String)
        println(docsPath)

        let subarray = fileManager.contentsOfDirectoryAtPath(subfolderPath, error: &error)
        println(subarray)
    }
TestinginProd
  • 1,085
  • 1
  • 15
  • 35
  • So it was simply a typo in the pathname. – rmaddy May 29 '15 at 23:40
  • https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html#//apple_ref/doc/uid/20000154-SW38 – Leo Dabus May 29 '15 at 23:41
  • https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/index.html#//apple_ref/occ/instm/NSString/stringByAppendingPathComponent: – Leo Dabus May 29 '15 at 23:41