Code Monkey home page Code Monkey logo

swiftyfilesystem's Introduction

SwiftyFileSystem

CI Status Version License Platform

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.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

SwiftyFileSystem is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftyFileSystem'

Usage

URL Extensions

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.

Simple FileManager Methods

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))

Data Size Formatting

import SwiftyFileSystem

// prints file size like "##.## MB" or "##.## GB" etc."
print("path/to/myfile".fileURL.fileSize.dataSizeString(decimals: 2, format: .short))

Author

NoodleOfDeath, [email protected]

License

SwiftyFileSystem is available under the MIT license. See the LICENSE file for more info.

swiftyfilesystem's People

Contributors

noodleofdeath avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.