简体   繁体   English

从目录中读取所有文件?

[英]Read all files from a directory?

I am trying to create a folder in my assets, then get a list of files inside.我正在尝试在我的资产中创建一个文件夹,然后获取其中的文件列表。 Sounds simple but there is no clean answer on how to do exactly this.听起来很简单,但没有明确的答案来说明如何做到这一点。

  1. Even to get the list from the main directory, most people can't do on Swift 3, reading here : Getting list of files in documents folder即使要从主目录中获取列表,大多数人在 Swift 3 上也做不到,请阅读此处: 获取文档文件夹中的文件列表

using :使用:

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)

Not working.不工作。

  1. Reading from a specific folder, I couldn't understand how to get it's path for swift.从特定文件夹读取,我无法理解如何快速获取它的路径。

Any example that really work that reads from a folder ?任何从文件夹中读取的真正有效的示例?

If you want to get all files in a personal directory, here is the simple answer如果您想获取个人目录中的所有文件,这里是简单的答案

    do {
        let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let Path = documentURL.appendingPathComponent("yourDirectoyName").absoluteURL
        let directoryContents = try FileManager.default.contentsOfDirectory(at: Path, includingPropertiesForKeys: nil, options: [])
    }
    catch {
        print(error.localizedDescription)
    }

And then if you want for example to read all files with special extension, you can do it that way然后,如果您想读取所有具有特殊扩展名的文件,您可以这样做

static func listAllFileNamesExtension(nameDirectory: String, extensionWanted: String) -> (names : [String], paths : [URL]) {

    let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
    let Path = documentURL.appendingPathComponent(nameDirectory).absoluteURL

    do {
        try FileManager.default.createDirectory(atPath: Path.relativePath, withIntermediateDirectories: true)
        // Get the directory contents urls (including subfolders urls)
        let directoryContents = try FileManager.default.contentsOfDirectory(at: Path, includingPropertiesForKeys: nil, options: [])

        // if you want to filter the directory contents you can do like this:
        let FilesPath = directoryContents.filter{ $0.pathExtension == extensionWanted }
        let FileNames = FilesPath.map{ $0.deletingPathExtension().lastPathComponent }

        return (names : FileNames, paths : FilesPath);

    } catch {
        print(error.localizedDescription)
    }

    return (names : [], paths : [])
}

So if you want to have all your json files in your personal directory因此,如果您想将所有 json 文件都放在您的个人目录中

let allJsonNamePath = listAllFileNamesExtension(nameDirectory:"yourDirectoryName", extensionWanted: "json")

Swift 4/5斯威夫特 4/5

let documentDirectoryPath:String = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let myFilesPath = "\(documentDirectoryPath)/myfolder"
let filemanager = FileManager.default
let files = filemanager.enumerator(atPath: myFilesPath)
while let file = files?.nextObject() {
    print(file)
}

Actually, amazingly, for 2 days no one could tell me the real issue here.实际上,令人惊讶的是,两天没有人可以告诉我这里的真正问题。 Creating a group , is completely different from dragging a folder into the project.创建,与文件夹拖入项目中完全不同。

For some reason, with Apple, its always complicated with files.出于某种原因,对于 Apple,文件总是很复杂。 I have to figure out the NOT so intuitive approach that a group that looks like a folder, is nothing but a nice way to look at something, and will not create a real folder accessible by the file manager.我必须找出一种不太直观的方法,即一个看起来像文件夹的组只不过是一种查看某些内容的好方法,并且不会创建一个可由文件管理器访问的真实文件夹。

This strange approach is maybe intutitive to a very pro programmer, but really not to any simple person.这种奇怪的方法对于非常专业的程序员来说可能是直观的,但对于任何简单的人来说真的不是。

Simply put, create a blue folder outside Xcode and drag it in.简单的说,在Xcode外面创建一个蓝色的文件夹,拖进去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM