swift macOS中的目录内容,不分解为软件包

xqnpmsa8  于 2023-02-03  发布在  Swift
关注(0)|答案(1)|浏览(91)

我试着用Swift遍历Mac电脑上的目录树,不想像应用程序一样进入软件包。
这样遍历目录树可以很好地工作:

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")
  }
}

尝试排除软件包不会返回任何结果,也不会出现错误:

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")
  }
}
55ooxyrt

55ooxyrt1#

在评论的帮助下,它的工作原理如下:

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

相关问题