📁 File is a powerful and intuitive file management library for Swift. It simplifies file and folder operations, providing a consistent API across different platforms. File is designed to make working with the file system a breeze, whether you're reading, writing, creating, or deleting files and folders.
File was deployed as Swift Package Manager. Package to install in a project. Add as a dependent item within the swift manifest.
let package = Package(
...
dependencies: [
.package(url: "https://github.com/pelagornis/swift-file", from: "1.1.0")
],
...
)Then import the File from thr location you want to use.
import FileThe documentation for releases and main are available here:
Path Setting.
let path = Path("/Users/ji-hoonahn/Desktop/") // exampleEasy access path.
Path.current
Path.root
Path.library
Path.temporary
Path.home
Path.documentsThis example demonstrates how to create a file and write a string to it.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.write("Hello, File!")This example shows how to write binary data to a file.
import Foundation
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "binary.data")
let data = Data([0x00, 0x01, 0x02, 0x03])
try file.write(data)This example demonstrates how to append a string to an existing file.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.append(" This is appended content.")This example shows how to append binary data to an existing file.
import Foundation
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "binary.data")
let data = Data([0x04, 0x05, 0x06, 0x07])
try file.append(data)This example demonstrates how to read data from a file.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.write("Hello, File!")
let readData = try file.read()
if let readString = String(data: readData, encoding: .utf8) {
print(readString) // Output: Hello, File!
}If AppKit is available, this example shows how to open a file using AppKit.
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
file.open()
#endifThis example shows how to catch and handle FileError.
import File
let path = Path.root
let filePath = Path("/path/that/do/not/exist/example.txt")
do {
let folder = try Folder(path: path)
let file = try folder.createFile(at: filePath)
try file.write("this will fail")
} catch let error as FileError {
print("FileError: \(error.message)")
if let underlyingError = error.error {
print("Underlying error: \(underlyingError)")
}
} catch {
print("Unexpected error: \(error)")
}This example demonstrates how to create subfolders and files within a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let subfolder = try folder.createSubfolder(at: "subfolder")
let newFile = try subfolder.createFile(at: "newFile.txt")
try newFile.write("Content of the new file")This example shows how to retrieve files and subfolders from a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
_ = try folder.createSubfolder(at: "subfolder")
_ = try folder.createFile(at: "example.txt")
let files = folder.files
let subfolders = folder.subfolders
print(files) // Output: Contains "example.txt"
print(subfolders) // Output: Contains "subfolder"This example shows how to move and copy the contents of a folder to another folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
_ = try folder.createFile(at: "example.txt")
_ = try folder.createSubfolder(at: "subfolder")
try folder.moveContents(to: destinationFolder)
let files = destinationFolder.files
let subfolders = destinationFolder.subfolders
print(files) // Output: Contains "example.txt"
print(subfolders) // Output: Contains "subfolder"
let copyFolder = try Folder(path: Path(path.rawValue + "copy"))
try destinationFolder.copy(to: copyFolder)This example shows how to empty a folder (delete all its contents).
import File
let path = Path.temporary
let folder = try Folder(path: path)
_ = try folder.createFile(at: "example.txt")
_ = try folder.createSubfolder(at: "subfolder")
try folder.empty()This example show how to delete subfolders and file
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
let subfolder = try folder.createSubfolder(at: "subfolder")
try file.delete()
try subfolder.delete()This example demonstrates how to rename a file or a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "oldName.txt")
try file.rename(to: "newName")
print(file.name) // Output: newName.txtThis example shows how to move a file or a folder to a new location.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
let file = try folder.createFile(at: "example.txt")
try file.move(to: destinationFolder)This example shows how to copy a file or a folder to a new location.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let destinationFolder = try Folder(path: Path(path.rawValue + "destination"))
let file = try folder.createFile(at: "example.txt")
try file.copy(to: destinationFolder)This example shows how to delete a file or a folder.
import File
let path = Path.temporary
let folder = try Folder(path: path)
let file = try folder.createFile(at: "example.txt")
try file.delete()Check if a file or folder exists:
let file = try folder.createFile(at: "existTest.txt")
if file.exists() {
print("File exists!")
}
if folder.exists() {
print("Folder exists!")
}Get all files and folders recursively:
let allFiles = folder.allFiles(recursive: true)
let allFolders = folder.allFolders(recursive: true)Create and check symbolic links:
let target = try folder.createFile(at: "target.txt")
let linkPath = folder.store.path.rawValue + "link.txt"
let linkStore = try Store<File>(path: Path(linkPath), fileManager: .default)
try linkStore.createSymbolicLink(to: target.store.path)
if linkStore.isSymbolicLink() {
print("This is a symbolic link!")
print("Destination: \(linkStore.destinationOfSymbolicLink()?.rawValue ?? "")")
}Get and set POSIX permissions:
let file = try folder.createFile(at: "perm.txt")
let originalPerm = file.store.getPermissions()
try file.store.setPermissions(0o600)
let newPerm = file.store.getPermissions()
print("New permissions: \(String(newPerm ?? 0, radix: 8))")Watch for changes to a file or folder:
#if os(macOS) || os(iOS)
let file = try folder.createFile(at: "watch.txt")
let source = file.store.watch {
print("File changed!")
}
try file.write("changed!")
// Don't forget to cancel the source when done:
if let src = source as? DispatchSourceFileSystemObject { src.cancel() }
#endifswift-file is under MIT license. See the LICENSE file for more info.