Directory
public class Directory: MutableFileSystemItem
A directory which exists in the local filesystem (at least at the time of initialisation).
-
If true, then you can only make changes to the file system in the current working directory, or any of its subdirectories.
Declaration
Swift
public static var sandbox = true -
The path to this directory.
Declaration
Swift
public internal(set) var path: DirectoryPath -
Opens an already existing directory.
Parameter
Parameter path: The path to the directory.Throws
FileSystemError.notFound, .notDirectory, .invalidAccess.Declaration
Swift
public required init(open path: DirectoryPath) throwsParameters
pathThe path to the directory.
-
Opens an already existing directory.
Parameter
Parameter stringpath: The string path to the directory.Throws
FileSystemError.notFound, .notDirectory, .invalidAccess.Declaration
Swift
public required convenience init(open stringpath: String) throwsParameters
stringpathThe string path to the directory.
-
Creates a new directory.
- Parameters:
- path: The path where the new directory should be created.
- ifExists: What to do if it already exists: open, throw error or replace.
Throws
FileSystemError.notDirectory, .alreadyExists, .outsideSandbox.Declaration
Swift
public init(create path: DirectoryPath, ifExists: AlreadyExistsOptions) throwsParameters
pathThe path where the new directory should be created.
ifExistsWhat to do if it already exists: open, throw error or replace.
- Parameters:
-
Creates a new directory.
- Parameters:
- path: The string path where the new directory should be created.
- ifExists: What to do if it already exists: open, throw error or replace.
Throws
FileSystemError.notDirectory, .alreadyExists, .outsideSandbox.Declaration
Swift
public convenience init(create stringpath: String, ifExists: AlreadyExistsOptions) throwsParameters
pathThe string path where the new directory should be created.
ifExistsWhat to do if it already exists: open, throw error or replace.
- Parameters:
-
Lists all directories under this directory matching the pattern.
- Parameters:
- pattern: A glob pattern, supporting wilcards
and
?
. The default is , matching everything. - recursive: If true, searches all subdirectories and their subdirectories etc. The default is
false.
Returns
An array of DirectoryPath.Declaration
Swift
public func directories(_ pattern: String = "*", recursive: Bool = false) -> [DirectoryPath]Parameters
patternA glob pattern, supporting wilcards “”, matching everything.
recursiveIf true, searches all subdirectories and their subdirectories etc. The default is
false.Return Value
An array of DirectoryPath.
- Parameters:
-
Lists all files under this directory matching the pattern.
- Parameters:
- pattern: A glob pattern, supporting wilcards
and
?
. The default is , matching everything. - recursive: If true, searches all subdirectories and their subdirectories etc. The default is
false.
Returns
An array of FilePath.Declaration
Swift
public func files(_ pattern: String = "*", recursive: Bool = false) -> [FilePath]Parameters
patternA glob pattern, supporting wilcards “”, matching everything.
recursiveIf true, searches all subdirectories and their subdirectories etc. The default is
false.Return Value
An array of FilePath.
- Parameters:
-
Returns true if there is a file or directory at ‘stringpath’ relative to this directory.
Declaration
Swift
public func contains(_ stringpath: String) -> Bool -
Throws an error if there is not a file or directory at ‘stringpath’ relative to this directory. - Throws: FileSystemError.notFound.
Declaration
Swift
public func verifyContains(_ stringpath: String) throws -
Creates a new file at ‘stringpath’, relative to this directory.
- Parameters:
- stringpath: The path where the new file should be created.
- ifExists: What to do if it already exists: open, throw error or replace.
Returns
A WritableFile ready to write to the new file.Throws
FileSystemError.isDirectory, .couldNotCreate, .alreadyExists, .outsideSandbox.Declaration
Swift
public func create(file stringpath: String, ifExists: AlreadyExistsOptions) throws -> WritableFileParameters
stringpathThe path where the new file should be created.
ifExistsWhat to do if it already exists: open, throw error or replace.
Return Value
A WritableFile ready to write to the new file.
- Parameters:
-
Creates a new directory at ‘stringpath’, relative to this directory.
- Parameters:
- stringpath: The path where the new directory should be created.
- ifExists: What to do if it already exists: open, throw error or replace.
Returns
A WritableFile ready to write to the new file.Throws
FileSystemError.notDirectory, .alreadyExists, .outsideSandbox.Declaration
Swift
public func create(directory stringpath: String, ifExists: AlreadyExistsOptions) throws -> DirectoryParameters
stringpathThe path where the new directory should be created.
ifExistsWhat to do if it already exists: open, throw error or replace.
Return Value
A WritableFile ready to write to the new file.
- Parameters:
-
Opens for reading the file at ‘stringpath’, relative to this directory.
Parameter
Parameter stringpath: the path to the file, relative to this directory.Returns
A ReadableFile ready to read from the file.Throws
FileSystemError.notFound, .isDirectory, .invalidAccess.Declaration
Swift
public func open(file stringpath: String) throws -> ReadableFileParameters
stringpaththe path to the file, relative to this directory.
Return Value
A ReadableFile ready to read from the file.
-
Opens for writing the file at ‘stringpath’, relative to this directory.
Parameter
Parameter stringpath: the path to the file, relative to this directory.Returns
A WritableFile ready to write to the file.Throws
FileSystemError.notFound, .isDirectory, .invalidAccess, .outsideSandbox.Declaration
Swift
public func edit(file stringpath: String) throws -> WritableFileParameters
stringpaththe path to the file, relative to this directory.
Return Value
A WritableFile ready to write to the file.
-
Opens the directory at ‘stringpath’, relative to this directory.
Parameter
Parameter stringpath: the path to the directory, relative to this directory.Returns
A Directory object.Throws
FileSystemError.notFound, .notDirectory, .invalidAccess.Declaration
Swift
public func open(directory stringpath: String) throws -> DirectoryParameters
stringpaththe path to the directory, relative to this directory.
Return Value
A Directory object.
-
Deletes this directory. For ever.
Throws
FileSystemError.outsideSandbox, NSError.Declaration
Swift
public func delete() throws
-
The current working directory.
Declaration
Swift
public static var current: Directory -
The current user’s home directory.
Declaration
Swift
public static var home: Directory -
The root directory in the local file system.
Declaration
Swift
public static var root: Directory -
Creates a new empty temporary directory, guaranteed to be unique every time.
Declaration
Swift
public static func createTempDirectory() -> Directory
-
Creates a symbolic link at ‘newlink’ pointing to the directory at ‘target’.
- Parameters:
- newlink: The path to the new symbolic link.
- target: The directory the new link should point to.
- ifExists: What to do if there already is something at newlink: open, throw error or replace.
Throws
FileSystemError.notDirectory, .alreadyExists, .outsideSandbox, .invalidAccess.Declaration
Swift
public convenience init(createSymbolicLink newlink: DirectoryPath, to target: Directory, ifExists: AlreadyExistsOptions) throwsParameters
newlinkThe path to the new symbolic link.
targetThe directory the new link should point to.
ifExistsWhat to do if there already is something at newlink: open, throw error or replace.
- Parameters:
-
Creates a symbolic link at ‘newlink’ pointing to the directory at ‘target’.
- Parameters:
- newlink: The path to the new symbolic link, relative to this directory.
- target: The directory the new link should point to.
- ifExists: What to do if there already is something at newlink: open, throw error or replace.
Throws
FileSystemError.notDirectory, .alreadyExists, .outsideSandbox, .invalidAccess.Declaration
Swift
public func create(symbolicLink newlink: String, to target: Directory, ifExists: AlreadyExistsOptions) throws -> DirectoryParameters
newlinkThe path to the new symbolic link, relative to this directory.
targetThe directory the new link should point to.
ifExistsWhat to do if there already is something at newlink: open, throw error or replace.
- Parameters:
-
Creates a symbolic link at ‘newlink’ pointing to the file at ‘target’.
- Parameters:
- newlink: The path to the new symbolic link, relative to this directory.
- target: The file the new link should point to.
- ifExists: What to do if there already is something at newlink: open, throw error or replace.
Throws
FileSystemError.isDirectory, .alreadyExists, .outsideSandbox, .invalidAccess.Declaration
Swift
public func create<NewFile:File, TargetFile:File>(symbolicLink newlink: String, to target: TargetFile, ifExists: AlreadyExistsOptions) throws -> NewFileParameters
newlinkThe path to the new symbolic link, relative to this directory.
targetThe file the new link should point to.
ifExistsWhat to do if there already is something at newlink: open, throw error or replace.
- Parameters:
View on GitHub
Directory Class Reference