SwiftyFileSystem is a lightweight wrapper framework around the Swift FileManager
class with several URL
extensions, built-in renaming policies when moving and copying files, common directory path/url generation methods, and file size string formatting.
To run the example project, clone the repo, and run pod install
from the Example directory first.
SwiftyFileSystem is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SwiftyFileSystem'
import SwiftyFileSystem
// Alias constructor for URL(fileURLWithPath: "path/to/myfile")
let url = "path/to/myfile".fileURL
// URL resource properties
print(url.fileExists) // prints `true` or `false`
print(url.isLocal) // prints `true` if the file resides on the local machine
print(url.isUbiquitous) // prints `true` if the file is managed and synced the cloud
print(url.isRegularFile) // prints `true` if the file is regular file
print(url.isDirectory) // prints `true` if the file is a directory
print(url.isSymbolicLink) // prints `true` if the file is a symbolic link
print(url.isInferredHidden) // prints `true` if the filename begins wit a "." or "~" character
print(url.isHidden) // prints `true` if the file hidden by default from the user
print(url.fileSize) // prints the size of this file in bytes, if it is a regular file
print(url.sizeOfContents) // prints the recurively summated size of a directory's contents
print(url.fileCount) // prints the number of files contained in a directory
print(url.creationDate) // prints the creation date of this file
print(url.contentAccessDate) // prints the content access date of this file
print(url.modificationDate) // prints the most recent modification date of this file
See Data Size Formatting for displaying file sizes as formatted strings.
import SwiftyFileSystem
// Enumerates and prints the file contents of a directory.
for file in FileSystem.contentsOfDirectory(at: FileSystem.mainResourcePath) {
print(file)
}
// Copy a file from one testDirectory to another, and increment the name if the
// destination file already exists using a renaming policy that puts the
// version number after the basename but before the file extension
// prefixed by a dash and bounded in parentheses.
let src = "path/to/my-src".fileURL
let dst = "path/to/my-dst".fileURL
FileSystem.copyItem(at: src, to: dst, with:
FileSystem.NamingPolicy(options:
[.versionDashed,
.versionInsideParentheses], maxRenamingAttempts: 10))
import SwiftyFileSystem
// prints file size like "##.## MB" or "##.## GB" etc."
print("path/to/myfile".fileURL.fileSize.dataSizeString(decimals: 2, format: .short))
NoodleOfDeath, git@noodleofdeath.com
SwiftyFileSystem is available under the MIT license. See the LICENSE file for more info.