1 Reply
      Latest reply on Apr 17, 2019 5:43 AM by OOPer
      Pendragon Level 1 Level 1 (0 points)

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

        • Re: Filemanager Enumerator depth ?
          OOPer Level 7 Level 7 (4,875 points)

          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)
              //...
          }