Path
public struct Path
A Path represents an absolute path on a filesystem.
All functions on Path are chainable to facilitate doing sequences of file operations.
Note
APath does not necessarily represent an actual filesystem entry.
-
The URL representation of the underlaying filesystem path.
Declaration
Swift
public let url: URL -
The normalized string representation of the underlying filesystem path.
Declaration
Swift
public var path: String { get } -
Creates a new absolute, standardized path from the provided file-scheme URL.
Throws
Path.Error.invalidURLSchemeif URL scheme is notfile.Declaration
Swift
public init(url: URL) throwsParameters
urlThe path URL.
-
Creates a new absolute, standardized path.
Throws
Path.Error.cannotResolvePathif path cannot be resolved.Note
- Resolves any
..or.components in path. - Resolves initial
~/with path to current user home directory. - Resolves initial
~user/with path to specified user home directory. - Does not resolve any symlinks.
- If provided path is relative (not prefixed with
/) then the resolved path is relative to the current working directory. - On macOS, removes an initial component of
/private/var/automount
,/var/automount
, or/private
from the path, if the result still indicates an existing file or directory.
Declaration
Swift
public init<S>(_ path: S) throws where S : StringProtocolParameters
pathThe string represented an absolute path
- Resolves any
-
Creates a new absolute, standardized path which is constructed from path which is relative to parent path.
Declaration
Swift
@inlinable public init<S>(_ path: S, relativeTo parent: Path) where S : StringProtocolParameters
pathA string which represends the relative part of the path.
parentThe parent of the target path.
-
The
Pathto the root directory.Declaration
Swift
static let root: Path -
The
Pathto the current working directory.Declaration
Swift
static var current: Path { get } -
The current user’s home
Declaration
Swift
static var home: Path { get } -
The system’s temporary directory.
Declaration
Swift
static var temporary: Path { get } -
Creates a new temporary directory, runs work closure and delete it at end.
Declaration
Swift
static func temporary<T>(_ work: (Path) throws -> T) throws -> TParameters
workThe closure with some logic related to the new tmp directory. The temporary directory is passed as an agument to this closure. Returned value from closure will be returned to the caller.
Return Value
The value returned from the work closure.
-
The parent directory for this path.
Declaration
Swift
var parent: Path { get } -
A sequence containing all of this folder’s filesystem items. Initially non-recursive and skip hidden items, use
recursiveorincludingHiddenon the returned sequence to change that.Declaration
Swift
var children: ChildSequence { get } -
The path components, or an empty array for root path.
Declaration
Swift
var pathComponents: [String] { get } -
The basename for this filesystem item including
extension.Declaration
Swift
@inlinable var basename: String { get } -
Returns the filename extension of this path.
Note
- If there is no extension returns empty string.
- If the filename ends with any number of
.
, returns empty string.
Declaration
Swift
var `extension`: String { get } -
The basename for this filesystem item without file
extension.Declaration
Swift
var basenameWithoutExtension: String { get } -
Returns a string representing the relative path to
base. Ifbaseis not a logical prefix forselfyour result will be prefixed some number of../components.Declaration
Swift
func path(relativeTo base: Path) -> StringParameters
baseThe base to which we calculate the relative path.
Return Value
The relative path to
base. -
Returns a new path made by appending a given path string.
Note
..and.components are interpreted.Declaration
Swift
func appending<S>(_ path: S?) -> Path where S : StringProtocolParameters
pathThe path to append to this path
Return Value
A new path made by appending path to the receiver.
-
Create a new path by appending the append string to the specified path.
Note
..and.components are interpreted.See also
appending(_:)Declaration
Swift
static func + <S>(path: Path, append: S?) -> Path where S : StringProtocolParameters
pathThe base path for the new one.
appendThe string path to join with specified path.
Return Value
A new joined path.
-
A type of a filesystem entry at this path. An
.unknownvalue is returned if the entry doesn’t exist or the type cannot be determined.Declaration
Swift
var type: EntryType { get } -
An attributes of a filesystem entry, or
nilif entry not exist.Declaration
Swift
var attributes: Attributes? { get }
-
Writes the text to file on this path.
Declaration
Swift
@discardableResult func write(text: String, append: Bool = false, encoding targetEncoding: String.Encoding = .utf8) throws -> PathParameters
textThe string content to write.
appendAppends the string to the end of file.
targetEncodingThe encoding in which the string should be interpreted.
Return Value
Selfto allow chaining. -
Writes the data to file on this path.
Declaration
Swift
@discardableResult func write(data: Data, append: Bool = false) throws -> PathParameters
dataThe data content to write.
appendAppends the data to the end of file.
Return Value
Selfto allow chaining.
-
Returns a Boolean value that indicates whether this path represents an actual filesystem entry. A
falsewas returned if the entry does not exist or its existence could not be determined.Declaration
Swift
var exist: Bool { get } -
Copies this filesystem item to the another destination. If the destination path is directory then the current item will be placed to that directory (
destination/self.basename).Throws
An error if file cannot be copied.Declaration
Swift
@discardableResult func copy(to destination: Path, overwrite: Bool = false) throws -> PathParameters
destinationThe new location for item on this path.
overwriteIf
trueoverwrites file on destination location.Return Value
A
Pathto copied item location. -
Moves this filesystem item to the another destination. If the destination path is directory then the current item will be moved into that directory (
destination/self.basename).Throws
An error if file cannot be moved.Declaration
Swift
@discardableResult func move(to destination: Path, overwrite: Bool = false) throws -> PathParameters
destinationThe new location for this item.
overwriteIf
trueoverwrites file on destination location.Return Value
A
Pathto moved item. -
Creates an empty file at this path or if the file exists, updates its modification time.
Note
If name is path and directories in this path not exists then throws error.Throws
An error if file cannot be created or modification time cannot be change.Declaration
Swift
@discardableResult func touch(_ name: String? = nil) throws -> PathParameters
nameThe name of the new file. If name not provided or
nilthen file will be created atself.path. If name is specified then new file path will beself.path.appending(name).Return Value
A
Pathto the new file. -
Creates a directory at this path or at new appended path if the name was supplied.
Throws
An error if directory cannot be created.Declaration
Swift
@discardableResult func createDirectory(_ name: String? = nil) throws -> PathParameters
nameThe name of the new directory or
nilif you want use this path. This name can be used to create intermediary directories.Return Value
A
Pathto the new directory. -
Deletes the path, recursively if a directory.
If path doesn’t exist do nothing. If entry is a symlink, deletes the symlink.
Declaration
Swift
func delete(useTrash: Bool = false) throwsParameters
useTrashMove the path to the trash insted of delete.
-
Renames the filesystem item at this path.
Throws
Error.invalidArgumentValueif new name contain directory separator.Declaration
Swift
@discardableResult func rename(to name: String) throws -> PathParameters
nameThe new name of this item.
Return Value
A
Pathto the renamed item.
-
Returns
Path.stringDeclaration
Swift
@inlinable public var description: String { get }
-
Creates an instance initialized to the given string value.
Do not call this initializer directly. It is used by the
CLI.ask(_:options:)andCLI.choose(_:chices:)functions.Declaration
Swift
@inlinable public init?(stringArgument: String)
-
Declaration
Swift
@inlinable public static func == (lhs: Path, rhs: Path) -> Bool -
Declaration
Swift
public func hash(into hasher: inout Hasher) -
Declaration
Swift
public static func < (lhs: Path, rhs: Path) -> Bool
-
Creates a new instance of path by decoding from the given decoder.
Declaration
Swift
public init(from decoder: Decoder) throwsParameters
decoderThe decoder to read data from.
-
Encodes this path into the given encoder.
Declaration
Swift
public func encode(to encoder: Encoder) throwsParameters
encoderThe encoder to write data to.
-
Filesystem entry attributes representation.
See moreDeclaration
Swift
struct Attributes -
The type of iterator used byt
See morePath.ChildSequence. Don’t interact with this type directly. SeePath.ChildSequencefor more information.Declaration
Swift
struct ChildSequenceIterator : IteratorProtocol -
A type of filesystem entry.
See moreDeclaration
Swift
enum EntryType : CaseIterable -
A filesystem entry permission representation.
See moreDeclaration
Swift
struct Permissions : Equatable, OptionSet, CustomStringConvertible
View on GitHub
Path Structure Reference