Swiftpack.co - Package - JohnSundell/Files

โ€œFilesโ€

BuddyBuild CocoaPods Carthage Marathon Swift Package Manager Twitter: @johnsundell

Welcome to Files, a compact library that provides a nicer way to handle files and folders in Swift. Itโ€™s primarily aimed at Swift scripting and tooling, but can also be embedded in applications that need to access the file system. It's essentially a thin wrapper around the FileManager APIs that Foundation provides.

Features

  • [X] Modern, object-oriented API for accessing, reading and writing files & folders.
  • [X] Unified, simple do, try, catch error handling.
  • [X] Easily construct recursive and flat sequences of files and folders.

Examples

Iterate over the files contained in a folder:

for file in try Folder(path: "MyFolder").files {
    print(file.name)
}

Rename all files contained in a folder:

try Folder(path: "MyFolder").files.enumerated().forEach { (index, file) in
    try file.rename(to: file.nameWithoutExtension + "\(index)")
}

Recursively iterate over all folders in a tree:

Folder.home.makeSubfolderSequence(recursive: true).forEach { folder in
    print("Name : \(folder.name), parent: \(folder.parent)")
}

Create, write and delete files and folders:

let folder = try Folder(path: "/users/john/folder")
let file = try folder.createFile(named: "file.json")
try file.write(string: "{\"hello\": \"world\"}")
try file.delete()
try folder.delete()

Move all files in a folder to another:

let originFolder = try Folder(path: "/users/john/folderA")
let targetFolder = try Folder(path: "/users/john/folderB")
try originFolder.files.move(to: targetFolder)

Easy access to system folders:

Folder.current
Folder.temporary
Folder.home

Usage

Files can be easily used in either a Swift script, command line tool or in an app for iOS, macOS, tvOS or Linux.

In a script

  • Install Marathon.
  • Add Files using $ marathon add https://github.com/johnsundell/files.git.
  • Run your script using $ marathon run <path-to-your-script>.

In a command line tool

  • Drag the file Files.swift into your command line tool's Xcode project.

In an application

Either

  • Drag the file Files.swift into your application's Xcode project.

or

  • Use CocoaPods, Carthage or the Swift Package manager to include Files as a dependency in your project.

Backstory

So, why was this made? As I've migrated most of my build tools and other scripts from languages like Bash, Ruby and Python to Swift, I've found myself lacking an easy way to deal with the file system. Sure, FileManager has a quite nice API, but it can be quite cumbersome to use because of its string-based nature, which makes simple scripts that move or rename files quickly become quite complex.

So, I made Files, to enable me to quickly handle files and folders, in an expressive way. And, since I love open source, I thought - why not package it up and share it with the community? :)

Questions or feedback?

Feel free to open an issue, or find me @johnsundell on Twitter.

Github

link
Stars: 1423
Help us keep the lights on

Dependencies

Releases

3.1.0 - Apr 5, 2019

  • Both File and Folder now have a path(relativeTo:) API, which makes it easy to calculate a given item's path relative to one of its parent folders.
  • FileSystem.documentFolder and FileSystem.libraryFolder are now only available on macOS.

View build details and download artifacts on buddybuild: Files (iOS, Files-iOS)

3.0.0 - Mar 26, 2019

Files now uses Swift 5.0, which makes it incompatible with toolchains with versions lower than 4.2.

View build details and download artifacts on buddybuild: Files (iOS, Files-iOS)

2.3.0 - Mar 15, 2019

  • New APIs have been added to check whether a given folder contains either another folder or a file (thanks @clayellis!)
  • The old, long since deprecated creatingSubfolderFailed error has now been fully removed.

View build details and download artifacts on buddybuild: Files (iOS, Files-iOS)

2.2.1 - Apr 10, 2018

This patch makes Files backwards compatible with Swift 4.0. Thanks @iainsmith ๐Ÿ™Œ

View build details and download artifacts on buddybuild: Files (iOS, Files-iOS)

2.2.0 - Apr 1, 2018

This release bumps Files' Swift version to 4.1. Thanks @krzyzanowskim! ๐Ÿ‘

View build details and download artifacts on buddybuild: Files (iOS, Files-iOS)