Filemanager Enumerator depth ?

Is there a way to specify directory depth using Filemanager (enumerator) ?

Accepted Reply

I cannot find any parameters specifying max depth in the `enumerator` methods of `FileManager`,

but the class `DirectoryEnumerator` has a property named `level` and you can manage it by yourself.


let depth = 3

let homeDir = FileManager.default.homeDirectoryForCurrentUser
let dirEnumerator = FileManager.default.enumerator(atPath: homeDir.path)!
for case let path as String in dirEnumerator {
    let attr = dirEnumerator.fileAttributes!
    let isDir = attr[.type] as! String == FileAttributeType.typeDirectory.rawValue
    if dirEnumerator.level == depth && isDir {
        dirEnumerator.skipDescendants()
    }
    
    print(path)
    //...
}

Replies

I cannot find any parameters specifying max depth in the `enumerator` methods of `FileManager`,

but the class `DirectoryEnumerator` has a property named `level` and you can manage it by yourself.


let depth = 3

let homeDir = FileManager.default.homeDirectoryForCurrentUser
let dirEnumerator = FileManager.default.enumerator(atPath: homeDir.path)!
for case let path as String in dirEnumerator {
    let attr = dirEnumerator.fileAttributes!
    let isDir = attr[.type] as! String == FileAttributeType.typeDirectory.rawValue
    if dirEnumerator.level == depth && isDir {
        dirEnumerator.skipDescendants()
    }
    
    print(path)
    //...
}