Skip to content

Instantly share code, notes, and snippets.

@dedeexe
Last active February 21, 2018 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dedeexe/4a9e87833630b01f69e8ffa0885027ab to your computer and use it in GitHub Desktop.
Save dedeexe/4a9e87833630b01f69e8ffa0885027ab to your computer and use it in GitHub Desktop.
Class to navigate and handle directories.
//
// 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