FileUtils
FileUtils copied to clipboard
Easy way to work with files, directories and paths in swift on macOS and linux.
FileUtils
Easy way to work with files, directories and paths in swift on macOS and linux.
Why?
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).
Usage
File
Create a file
File(path: path, fileMode: .write)
// or
File.create(path: path)
Delete a file
File.delete(atPath: path)
Read file content
let content = File.read(atPath: path)
// or
let content = 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)
Path
Get temporary file and directory
let tmp = Path.tempPath
// or
let tmp = Path.tempFile
// or
let tmp = Path.tempFileName(withName: "abc.txt")
Get the current directory
let path = Path.currentDirectory
Get the home directory
let path = Path.home
Check if the path exists
let exists = Path.exists(path)
Check the type of the file at a path
let type = 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
let expanded = Path.expand("~/Documents")
// expanded is "/Users/YourUser/Documents"
Get the base name and the directory name of a path
let base = Path.baseName(forPath: "/Documents/this/is/mypath")
// base is "mypath"
let dir = Path.dirName(forPath: "/Documents/this/is/mypath")
// dir is "/Documents/this/is"
Directory
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
Installation
You can install File using Swift package manager (SPM) and Carthage
Swift Package Manager
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),
]
)
Carthage
github "getGuaka/FileUtils"
Tests
Tests can be found here.
Run them with
swift test
Todo
- Handle non textual files contents
Contributing
Just send a PR! We don't bite ;)