Easy way to work with files, directories and paths in swift on macOS and linux.
You are developing a cli and you want to:
- Read/Write files.
- Create/Delete files.
- Create/Delete/List directories.
- Get different paths (Home/Current/Temp).
- Get the base name and directory name of a path
You can use FileUtils with Guaka to create aweseome command line applications.
Note: At the moment, this library only deals with textual files contents. (Check todo section of this file).
Create a file
File(path: path, fileMode:.write)
// or
File.create(path: path)Delete a file
File.delete(atPath: path)Read file content
letcontent=File.read(atPath: path)
// or
letcontent=String.read(contentsOfFile: path)Write file content
File.write(string:"ABCDEF", toPath: path,)
// or
"AAAAA".write(toFile: path)Check if file exists
File.exists(path)Get temporary file and directory
lettmp=Path.tempPath
// or
lettmp=Path.tempFile
// or
lettmp=Path.tempFileName(withName:"abc.txt")Get the current directory
letpath=Path.currentDirectoryGet the home directory
letpath=Path.homeCheck if the path exists
letexists=Path.exists(path)Check the type of the file at a path
lettype=Path.type(ofPath: path)type is a member of the PathType enum. This enum defines directory, executable, link and file
Expand a tilde in the path
letexpanded=Path.expand("~/Documents")
// expanded is "/Users/YourUser/Documents"Get the base name and the directory name of a path
letbase=Path.baseName(forPath:"/Documents/this/is/mypath")
// base is "mypath"
letdir=Path.dirName(forPath:"/Documents/this/is/mypath")
// dir is "/Documents/this/is"Create a directory
Directory.create(atPath: path)Delete a directory
Directory.delete(atPath: path)Enumerate contents of a directory
let(files, directories)=Directory.contents(ofDirectory: path)!this returns a tuple that contains all the files and directories found at the path
You can install File using Swift package manager (SPM) and Carthage
Add FileSystem as a dependency in your Package.swift
import PackageDescription
let package = Package(name: "YourPackage",
dependencies: [
.Package(url: "https://github.com/getGuaka/FileUtils.git", majorVersion: 0),
]
)
github "getGuaka/FileUtils"
Tests can be found here.
Run them with
swift test
- Handle non textual files contents
Just send a PR! We don't bite ;)