AngryDuck
AngryDuck

Reputation: 4607

Get All folder names from a path in documents directory swift

I am trying to get a [String] that contains the names of all the folders in a folder i have created in the documents directory. I currently have something working but it is iterating everything in those directories too and giving me the names of the files.

Currently i have this:

let file_manager = NSFileManager.defaultManager()
let enumerator:NSDirectoryEnumerator? = file_manager.enumeratorAtPath(getDocumentsFilePath("Test"))
while let element = enumerator?.nextObject() as? String
{
      // Add folder names to the return value
      return_value.insert(element, atIndex: return_value.count)
}

but as i said this is giving me the names of files in the directories too.

I found this code which seems to be what I'm looking for but it seems to work off a NSParentSearchPathDirectory, but this seems like it would give me all directory names in the documents directory. Wondering if this is what I'm looking for but if so how can i convert my string (path to the folder i want directory names from) to an NSParentSearchPathDirectory. Or if my first example is correct how would i only make it return directories and not file names too.

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory NSSearchPathDomainMask.AllDomainsMask, true) as? [String]

Upvotes: 1

Views: 6681

Answers (3)

kalpesh
kalpesh

Reputation: 1287

In Swift 4.0

func contentsOfDirectoryAtPath(path: String) -> [String]? {
        guard let paths = try? FileManager.default.contentsOfDirectory(atPath: path) else { return nil}
        return paths.map { aContent in (path as NSString).appendingPathComponent(aContent)}
    }
    func getListOfDirectories()->[String]?{

        let searchPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).last!
        let allContents = contentsOfDirectoryAtPath(path: searchPath)
        return allContents
    }

Upvotes: 0

sbooth
sbooth

Reputation: 16986

Here is another way without the string operations:

var subdirs = [NSURL]()

let enumerator = NSFileManager.defaultManager().enumeratorAtURL(
    NSURL.init(fileURLWithPath: "/System/Library", isDirectory: true),
    includingPropertiesForKeys: [NSURLIsDirectoryKey],
    options: .SkipsSubdirectoryDescendants,
    errorHandler: nil)

while let url = enumerator?.nextObject() as? NSURL {
    do {
        var resourceValue: AnyObject?
        try url.getResourceValue(&resourceValue, forKey: NSURLIsDirectoryKey)
        if let isDirectory = resourceValue as? Bool where isDirectory == true {
            subdirs.append(url)
        }
    }
    catch let error as NSError {
        print("Error: ", error.localizedDescription)
    }
}

Upvotes: 2

Sandeep
Sandeep

Reputation: 21154

Here is a simple function that you can use to get paths of all contents in certain directory.

func contentsOfDirectoryAtPath(path: String) -> [String]? {
    guard let paths = try? NSFileManager.defaultManager().contentsOfDirectoryAtPath(path) else { return nil}
   return paths.map { aContent in (path as NSString).stringByAppendingPathComponent(aContent)}
}

let searchPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last!

let allContents = contentsOfDirectoryAtPath(searchPath)

Upvotes: 6

Related Questions