Home > Enterprise >  Directory Contents in macOS without descending into packages
Directory Contents in macOS without descending into packages

Time:02-01

I try to traverse a directory tree on a Mac with Swift and do not want to decent into packages like apps.

Traversing the directory tree like this works fine:

func traverseDirectoryTree(){
  let path = "/Users/mica/Downloads/TEST"
  let url = URL(string: path)
  
  print("try to scan Dir")
  if let enumerator = FileManager().enumerator(atPath: path) {        
    for file in enumerator {
      let fileAttributes = enumerator.fileAttributes
      
      guard let fileName = file as? String else {continue}
      print("\(fileName)")
      if let fileAttributes = fileAttributes {
        print("   Size:\(fileAttributes[.size]! )")
        print("   Tpye:\(fileAttributes[.type]! )")
        print("   Tpye:\(fileAttributes[.referenceCount]! )")
      }
      print("")
    }
  } else {
    print("creating enumerator failed")
  }
}

Trying to exclude the packages returns nothing, no error:

func traverseDirectoryTree(){
  let path = "/Users/mica/Downloads/TEST"
  //let url = URL(string: path)
  let url = URL(filePath: path)
  
  print("try to scan Dir")
//  if let enumerator = FileManager().enumerator(atPath: path) {

  if let enumerator = FileManager().enumerator( at: url!,
                                                includingPropertiesForKeys: [.nameKey, .isDirectoryKey, .fileSizeKey],
                                                options: [.skipsPackageDescendants]
                                               ){

          
    for file in enumerator {
      let fileAttributes = enumerator.fileAttributes
      
      guard let fileName = file as? String else {continue}
      print("\(fileName)")
      if let fileAttributes = fileAttributes {
        print("   Size:\(fileAttributes[.size]! )")
        print("   Tpye:\(fileAttributes[.type]! )")
        print("   Tpye:\(fileAttributes[.referenceCount]! )")
      }
      print("")
    }
  } else {
    print("creating enumerator failed")
  }
}

CodePudding user response:

With the helps of the comments, it works like this:

  let startURL = URL(fileURLWithPath: "somePath")
  do
  { let enumerator = FileManager.default.enumerator(at: startURL,
                            includingPropertiesForKeys: [ .contentModificationDateKey, .fileSizeKey, .isRegularFileKey],
                                               options: [ .skipsHiddenFiles,
                                                          .skipsPackageDescendants],
                                          errorHandler: { (url, error) -> Bool in
                                                          print("directoryEnumerator error at \(url): ", error)
                                                          return true
                                                        }
    )!
    for item in enumerator {
      let fileURL = item as! URL
      let resourceValues = try fileURL.resourceValues(forKeys: Set([.creationDateKey, .isDirectoryKey, .fileSizeKey, .isRegularFileKey]))
      if (resourceValues.isRegularFile!) {
        if let size = resourceValues.fileSize {
         // do something ....
        }
      }
    }
  } catch
  { print(error)
  }
  • Related