Dir

Undocumented

  • Returns the path parameter passed to dir’s constructor.

    Declaration

    Swift

    public let path: String
  • Returns the file descriptor used in dir. This method uses dirfd() function defined by POSIX 2008.

    Declaration

    Swift

    public var fileno: Int
  • pos

    Returns the current position in dir. See also Dir#seek(pos:).

    Declaration

    Swift

    public var pos: Int
  • Returns the current position in dir.

    Declaration

    Swift

    public var tell: Int
  • Returns the path parameter passed to dir’s constructor.

    Declaration

    Swift

    public var to_path: String
  • Return a string describing this Dir object.

    Declaration

    Swift

    public var inspect: String
  • Return a string describing this Dir object.

    Declaration

    Swift

    public var description: String
  • Returns a new directory object for the named directory.

    Declaration

    Swift

    public init?(_ path: String)
  • Returns a new directory object for the named directory.

    Declaration

    Swift

    public static func new(_ path: String) -> Dir?

    Parameters

    path

    A directory path.

    Return Value

    A new Dir object or nil.

  • Returns a new directory object for the named directory.

    Declaration

    Swift

    public static func open(_ path: String) -> Dir?

    Parameters

    path

    A directory path.

    Return Value

    A new Dir object or nil.

  • Returns the path to the current working directory of this process as a string.

    Declaration

    Swift

    public static var getwd: String
  • pwd

    Returns the path to the current working directory of this process as a string. An alias to Dir#getwd var.

    Declaration

    Swift

    public static var pwd: String
  • Returns the home directory of the current user or the named user if given.

    Dir.home       #=> "/Users/username"
    

    Declaration

    Swift

    public static var home: String
  • Error throws when user doesn’t exists.

    • notExists: User doesn’t exists
    See more

    Declaration

    Swift

    public enum HomeDirectory: Error
  • Returns the home directory of the current user or the named user if given.

    Dir.home()       #=> "/Users/username"
    Dir.home("user") #=> "/user"
    

    Throws

    HomeDirectory.notExists if user doesn’t exist.

    Declaration

    Swift

    public static func home(_ user: String? = nil) throws -> String

    Parameters

    path

    The name of user.

    Return Value

    Home directory.

  • Changes the current working directory of the process to the given string.

    Dir.chdir("/var/spool/mail")
    Dir.pwd     #=> "/var/spool/mail"
    

    Declaration

    Swift

    public static func chdir(_ path: String = "~") -> Bool

    Parameters

    path

    A new current working directory.

    Return Value

    A bool indicates the result of Dir#chdir(path:)

  • Changes this process’s idea of the file system root.

    See also

    Darwin.chroot.

    Declaration

    Swift

    public static func chroot(_ path: String) -> Bool

    Parameters

    path

    A directory path.

    Return Value

    A bool value indicates the result of Darwin.chroot

  • Changes the current working directory of the process to the given string. Block is passed the name of the new current directory, and the block is executed with that as the current directory. The original working directory is restored when the block exits. The return value of chdir is the value of the block.

    Dir.pwd         #=> "/work"
    let value = Dir.chdir("/var") {
        Dir.pwd     #=> "/var"
        return 1
    }
    Dir.pwd         #=> "/work"
    value   #=> 1
    

    Declaration

    Swift

    public static func chdir<T>(_ path: String = "~", closure: ((Void) -> T)) -> T

    Parameters

    path

    A new current working directory.

    closure

    A block executed in target directory.

    Return Value

    The value of the closure.

  • Makes a new directory named by string, with permissions specified by the optional parameter anInteger.

    try Dir.mkdir("draveness")
    try! Dir.mkdir("draveness/spool/mail", recursive: true)
    

    Throws

    When FileManager#createDirectory(atPath:withIntermediateDirectories:attributes:) throws.

    Declaration

    Swift

    public static func mkdir(_ path: String, recursive: Bool = false, _ permissions: Int? = nil) throws

    Parameters

    path

    A new directory path.

    recursive

    Whether creats intermeidate directories.

    permissions

    An integer represents the posix permission.

  • Error throws when called Dir#rmdir(path:) method.

    • notEmpty: Directory is not empty.
    • notExists: Directory is not exists.
    See more

    Declaration

    Swift

    public enum RemoveDirectoryError: Error
  • Deletes the named directory.

    try Dir.rmdir("/a/folder/path")
    

    Throws

    RemoveDirectoryError if the directory isn’t empty.

    Declaration

    Swift

    public static func rmdir(_ path: String) throws

    Parameters

    path

    A directory path

  • Deletes the named directory. An alias to Dir.rmdir(path:).

    try Dir.unlink("/a/folder/path")
    

    Throws

    RemoveDirectoryError if the directory isn’t empty.

    Declaration

    Swift

    public static func unlink(_ path: String) throws

    Parameters

    path

    A directory path

  • Returns true if the named file is a directory, false otherwise.

    Dir.isExist("/a/folder/path/not/exists")     #=> false
    Dir.isExist("/a/folder/path/exists")         #=> true
    

    Declaration

    Swift

    public static func isExist(_ path: String) -> Bool

    Parameters

    path

    A file path.

    Return Value

    A bool value indicates whether there is a directory in given path.

  • Returns true if the named file is an empty directory, false if it is not a directory or non-empty.

    Dir.isEmpty("/a/empty/folder")           #=> true
    Dir.isEmpty("/a/folder/not/exists")      #=> true
    Dir.isEmpty("/a/folder/with/files")      #=> false
    

    Declaration

    Swift

    public static func isEmpty(_ path: String) -> Bool

    Parameters

    path

    A directory path.

    Return Value

    A bool value indicates the directory is not exists or is empty.

  • Returns an array containing all of the filenames in the given directory. Will return an empty array if the named directory doesn’t exist.

    Declaration

    Swift

    public static func entries(_ path: String) -> [String]

    Parameters

    path

    A directory path.

    Return Value

    An array of all filenames in the given directory.

  • Calls the block once for each entry in the named directory, passing the filename of each entry as a parameter to the block.

    Declaration

    Swift

    public static func foreach(_ path: String, closure: (String) -> ())

    Parameters

    path

    A directory path.

    closure

    A closure accepts all the entries in current directory.

  • Deletes the named directory.

    Declaration

    Swift

    public static func delete(_ path: String) -> Bool

    Parameters

    path

    A directory path.

    Return Value

    A bool value indicates the result of Dir.delete(path:)

  • Seeks to a particular location in dir.

    Declaration

    Swift

    public func seek(_ pos: Int)

    Parameters

    pos

    A particular location.

  • Repositions dir to the first entry.

    Declaration

    Swift

    public func rewind()