Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

A nicer way to handle files & folders in Swift

License

NotificationsYou must be signed in to change notification settings

JohnSundell/Files

Repository files navigation

“Files”

BuddyBuildCocoaPodsCarthageSwift Package ManagerTwitter: @johnsundell

Welcome toFiles, a compact library that provides a nicer way to handlefiles andfolders 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 theFileManager APIs thatFoundation provides.

Features

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

Examples

Iterate over the files contained in a folder:

forfileintryFolder(path:"MyFolder").files{print(file.name)}

Rename all files contained in a folder:

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

Recursively iterate over all folders in a tree:

Folder.home.subfolders.recursive.forEach{ folderinprint("Name :\(folder.name), parent:\(folder.parent)")}

Create, write and delete files and folders:

letfolder=tryFolder(path:"/users/john/folder")letfile=try folder.createFile(named:"file.json")try file.write("{\"hello\":\"world\"}")try file.delete()try folder.delete()

Move all files in a folder to another:

letoriginFolder=tryFolder(path:"/users/john/folderA")lettargetFolder=tryFolder(path:"/users/john/folderB")try originFolder.files.move(to: targetFolder)

Easy access to system folders:

Folder.currentFolder.rootFolder.libraryFolder.temporaryFolder.homeFolder.documents

Installation

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

Using the Swift Package Manager (preferred)

To install Files for use in a Swift Package Manager-powered tool, script or server-side application, add Files as a dependency to yourPackage.swift file. For more information, please see theSwift Package Manager documentation.

.package(url:"https://github.com/JohnSundell/Files", from:"4.0.0")

Using CocoaPods or Carthage

Please refer toCocoaPods’ orCarthage’s own documentation for instructions on how to add dependencies using those tools.

As a file

Since all of Files is implemented within a single file, you can easily use it in any project by simply dragging the fileFiles.swift into your Xcode 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 madeFiles, 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 toopen an issue, or find me@johnsundell on Twitter.

About

A nicer way to handle files & folders in Swift

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors20


[8]ページ先頭

©2009-2025 Movatter.jp