Files

repository·master·Indexed 25 days ago

https://github.com/johnsundell/files

A compact Swift library providing an object-oriented wrapper around Foundation's FileManager APIs. It simplifies file and folder manipulation for Swift scripting and tooling, offering features for recursive directory traversal, file lifecycle management, and access to common system folders.

Tokens
664
Snippets
5
Records
7
Agent score
33%

What's inside Files

  1. Install Files via Swift Package Manager

    master

    To use Files in a Swift Package Manager-powered tool, script, or server-side application, add it as a dependency to your Package.swift file.

    .package(url: "https://github.com/JohnSundell/Files", from: "4.0.0")
  2. Create, write, and delete files and folders

    master

    Files provides an object-oriented way to manage the lifecycle of files and directories using createFile(named:), write(_:), and delete().

    let folder = try Folder(path: "/users/john/folder")
    let file = try folder.createFile(named: "file.json")
    try file.write("{\"hello\": \"world\"}")
    try file.delete()
    try folder.delete()
  3. Iterate over files and folders

    master

    You can iterate over the immediate files in a folder or use the .recursive property to traverse an entire directory tree.

    // Iterate over files in a folder
    for file in try Folder(path: "MyFolder").files {
        print(file.name)
    }
    
    // Recursively iterate over all subfolders in a tree
    Folder.home.subfolders.recursive.forEach { folder in
        print("Name : \(folder.name), parent: \(folder.parent)")
    }
  4. Move files between folders

    master

    You can move a collection of files from one folder to another using the .move(to:) method on a sequence of files.

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