Last active
February 21, 2018 21:14
-
-
Save dedeexe/4a9e87833630b01f69e8ffa0885027ab to your computer and use it in GitHub Desktop.
Class to navigate and handle directories.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Directory.swift | |
// BackgroundApp | |
// | |
// Created by dede.exe on 21/02/18. | |
// Copyright © 2018 dede.exe. All rights reserved. | |
// | |
struct Directory { | |
public let path : String | |
public init(path : String) { | |
self.path = path | |
} | |
var exists : Bool { | |
return FileManager.default.fileExists(atPath: path) | |
} | |
public func create(dir name:String) -> Directory { | |
let fullPath = path + "/" + name | |
let url = URL(fileURLWithPath: fullPath) | |
let dir = Directory(path: fullPath) | |
do { | |
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) | |
return dir | |
} catch { | |
return dir | |
} | |
} | |
public func dir(_ name:String) -> Directory { | |
let path = self.path + "/" + name | |
return Directory(path: path) | |
} | |
static var home : Directory { | |
return Directory(path: NSHomeDirectory()) | |
} | |
} | |
// ============ Usage ============ | |
// Instantiating Directory | |
//let dir = Directory(path: "/") | |
// Instantiate a Directory pointing to home | |
//let dirHome = Directory.home | |
//Creating a new "files" directory inside home directory using Directory instance | |
//let newDir = dirHome.create(dir: "files") | |
//Check if new dir exists | |
//let success = newDir.exists |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment