获取文档文件夹中的文件列表


124

我在文档文件夹中获取文件名的代码有什么问题?

func listFilesFromDocumentsFolder() -> [NSString]?{
    var theError = NSErrorPointer()
    let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
    if dirs != nil {
        let dir = dirs![0] as NSString
        let fileList = NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir, error: theError) as [NSString]
        return fileList
    }else{
        return nil
    }
}

我以为我已正确阅读了文档,并且我对documents文件夹中的内容非常确定,但是“ fileList”没有显示任何内容?“目录”显示文件夹的路径。


您要显示文档目录中文件的名称还是文件夹中的文件?
Adeel Ur Rehman 2014年

首先,只有文档文件夹本身中的文件!
pawi 2014年

我刚运行了您的代码,它按预期工作。我在文档目录中得到了一份清单。我们会误解您的问题吗?
jwlaughton 2014年

奇怪?我还使用了“ .fileExistsAtPath(path)”,它告诉我存在一个特定的文件。但是“ fileList”中什么都没有显示!
pawi 2014年

我的清单包括目录和文件。
jwlaughton 2014年

Answers:


111

此解决方案适用于Swift 4(Xcode 9.2)和Swift 5(Xcode 10.2.1+):

let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
    let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
    // process files
} catch {
    print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}

这是一个可重用的FileManager扩展,它还允许您跳过或在结果中包含隐藏文件:

import Foundation

extension FileManager {
    func urls(for directory: FileManager.SearchPathDirectory, skipsHiddenFiles: Bool = true ) -> [URL]? {
        let documentsURL = urls(for: directory, in: .userDomainMask)[0]
        let fileURLs = try? contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil, options: skipsHiddenFiles ? .skipsHiddenFiles : [] )
        return fileURLs
    }
}

// Usage
print(FileManager.default.urls(for: .documentDirectory) ?? "none")

如何排除隐藏文件?
BasedMusa

251

斯威夫特5

// Get the document directory url
let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

do {
    // Get the directory contents urls (including subfolders urls)
    let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil)
    print(directoryContents)

    // if you want to filter the directory contents you can do like this:
    let mp3Files = directoryContents.filter{ $0.pathExtension == "mp3" }
    print("mp3 urls:",mp3Files)
    let mp3FileNames = mp3Files.map{ $0.deletingPathExtension().lastPathComponent }
    print("mp3 list:", mp3FileNames)

} catch {
    print(error)
}

1
对于像我一样粗心的人。请注意,在Swift 3.0中,由于未知原因,调用FileManager.default.contentsOfDirectory(atPath:<#T ## String#>)可能无法正常工作。在此使用上面给定的那个。
迈克尔·尚

如果我在iCloud Drive目录上运行此文件,它将仅返回已下载到设备的项目-有什么方法可以获取所有项目的URL,而无需先下载它们?
mralexhay

@mralexhay它为我显示了它,即使未下载的项目也是如此。文件名中应显示点.前缀和.cloud后缀。尝试let icloudFiles = directoryContents.filter{ $0.pathExtension == "icloud" }它应该显示未下载的文件
Leo Dabus

1
@LeoDabus感谢您的答复-我还没有意识到它的前缀是“。”,我认为它只是带有“ iCloud”后缀。我意识到我没有返回隐藏文件-难怪它们没有显示!再次感谢。
mralexhay

17

SWIFT 3的较短语法

func listFilesFromDocumentsFolder() -> [String]?
{
    let fileMngr = FileManager.default;

    // Full path to documents directory
    let docs = fileMngr.urls(for: .documentDirectory, in: .userDomainMask)[0].path

    // List all contents of directory and return as [String] OR nil if failed
    return try? fileMngr.contentsOfDirectory(atPath:docs)
}

用法示例:

override func viewDidLoad()
{
    print(listFilesFromDocumentsFolder())
}

在适用于iOS 10.2的iPhone 7和适用于iOS 9.3的iPad的xCode 8.2.3上进行测试


6

苹果声明NSSearchPathForDirectoriesInDomains(_:_:_:)

您应该考虑使用FileManager方法urls(for:in:)以及url(for:in:appropriateFor:create:)哪些方法返回URL,这是首选格式。


对于Swift 5,FileManager有一种称为的方法contentsOfDirectory(at:includingPropertiesForKeys:options:)contentsOfDirectory(at:includingPropertiesForKeys:options:)具有以下声明:

对指定目录执行浅层搜索,并返回包含项的URL。

func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?, options mask: FileManager.DirectoryEnumerationOptions = []) throws -> [URL]

因此,为了检索documents目录中包含的文件的url,可以使用下面的代码片段: FileManagerurls(for:in:)contentsOfDirectory(at:includingPropertiesForKeys:options:)方法的:

guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

do {
    let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])

    // Print the urls of the files contained in the documents directory
    print(directoryContents)
} catch {
    print("Could not search for urls of files in documents directory: \(error)")
}

例如,以下UIViewController实现显示了如何将应用程序捆绑包中的文件保存到文档目录,以及如何获取保存在文档目录中的文件的网址:

import UIKit

class ViewController: UIViewController {

    @IBAction func copyFile(_ sender: UIButton) {
        // Get file url
        guard let fileUrl = Bundle.main.url(forResource: "Movie", withExtension: "mov") else { return }

        // Create a destination url in document directory for file
        guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
        let documentDirectoryFileUrl = documentsDirectory.appendingPathComponent("Movie.mov")

        // Copy file to document directory
        if !FileManager.default.fileExists(atPath: documentDirectoryFileUrl.path) {
            do {
                try FileManager.default.copyItem(at: fileUrl, to: documentDirectoryFileUrl)
                print("Copy item succeeded")
            } catch {
                print("Could not copy file: \(error)")
            }
        }
    }

    @IBAction func displayUrls(_ sender: UIButton) {
        guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }

        do {
            let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsDirectory, includingPropertiesForKeys: nil, options: [])

            // Print the urls of the files contained in the documents directory
            print(directoryContents) // may print [] or [file:///private/var/mobile/Containers/Data/Application/.../Documents/Movie.mov]
        } catch {
            print("Could not search for urls of files in documents directory: \(error)")
        }
    }

}

5

此代码打印出我的文档目录中的所有目录和文件:

对功能的一些修改:

func listFilesFromDocumentsFolder() -> [String]
{
    let dirs = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.allDomainsMask, true)
    if dirs != [] {
        let dir = dirs[0]
        let fileList = try! FileManager.default.contentsOfDirectory(atPath: dir)
        return fileList
    }else{
        let fileList = [""]
        return fileList
    }
}

调用者:

    let fileManager:FileManager = FileManager.default
    let fileList = listFilesFromDocumentsFolder()

    let count = fileList.count

    for i in 0..<count
    {
        if fileManager.fileExists(atPath: fileList[i]) != true
        {
            print("File is \(fileList[i])")
        }
    }

4

Swift 2.0兼容性

func listWithFilter () {

    let fileManager = NSFileManager.defaultManager()
           // We need just to get the documents folder url
    let documentsUrl = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] as NSURL

    do {
    // if you want to filter the directory contents you can do like this:
    if let directoryUrls = try? NSFileManager.defaultManager().contentsOfDirectoryAtURL(documentsUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsSubdirectoryDescendants) {
        print(directoryUrls)
       ........
        }

    }

}

要么

 func listFiles() -> [String] {

    var theError = NSErrorPointer()
    let dirs = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true) as? [String]
    if dirs != nil {
        let dir = dirs![0]
        do {
        let fileList = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(dir)
        return fileList as [String]
        }catch {

        }

    }else{
        let fileList = [""]
        return fileList
    }
    let fileList = [""]
    return fileList

}

0

简单而动态的解决方案(Swift 5):

extension FileManager {

  class func directoryUrl() -> URL? {
      let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
      return paths.first
  }

  class func allRecordedData() -> [URL]? {
     if let documentsUrl = FileManager.directoryUrl() {
        do {
            let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil)
            return directoryContents.filter{ $0.pathExtension == "m4a" }
        } catch {
            return nil
        }
     }
     return nil
  }}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.