Manipulating Paths

The following functions and their PathRepresentable counterparts help you alter the file system.

  • Declaration

    Swift

    public func deletePath(_ path: String, recursive: Bool = true) throws
  • Move the file or directory from path to a new location. If new path exists, it is first removed. Both path must be of the same file type (directories, or non-directories) and must reside on the same file system. If the final component of old is a symbolic link, the symbolic link is renamed, not the file or directory to which it points.

    Throws

    System error. This could be caused by lack of permissions in either path, paths having different file types; attempting to move . or .., etc.

    See also

    To work with Path or PathRepresentable, use PathRepresentable.move(to:).

    Declaration

    Swift

    public func movePath(_ source: String, toPath destination: String) throws

    Parameters

    source

    The original location.

    destination

    The new location.

  • Create directory at given path.

    Throws

    A SystemError.

    Declaration

    Swift

    public func createDirectory(atPath path: String, permission: FilePermission = 0o0755, createParents: Bool = false, throwIfAlreadyExists: Bool = false) throws

    Parameters

    path

    The path at witch the directory is to be created.

    permissions

    Access flags (combined with the process’s umask) for the directory to be created.

    createParents

    If true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command). If false, a missing parent will cause a SystemError. Defaults to false

    throwIfAlreadyExists

    If true, a SystemError is thrown if the target directory (or any of it’s parent, if it needs creation) already exists.