Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

26 Commits

Repository files navigation

FileUtils

Build StatuscodecovPlatformLanguage: SwiftCarthage

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

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)

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

Get the home directory

letpath=Path.home

Check 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"

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 ;)

About

Easy way to work with files, directories and paths in swift on macOS and linux.

Resources

Stars

33 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages