简体   繁体   中英

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

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

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

Swift 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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