Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 253
Curnelious Avatar asked Jan 25 '18 09:01

Curnelious


People also ask

How do I read all files in a folder?

The listFiles() method of the File class returns an array holding the objects (abstract paths) of all the files (and directories) in the path represented by the current (File) object. Create a file object by passing the required file path as a parameter. Read the contents of each file using Scanner or any other reader.

How do I read all files in a folder in R?

To list all files in a directory in R programming language we use list. files(). This function produces a list containing the names of files in the named directory. It returns a character vector containing the names of the files in the specified directories.

How do I read a list of files in a directory in Python?

To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.


1 Answers

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")
like image 138
Sacha.R Avatar answered Oct 01 '22 20:10

Sacha.R