DirAccess
Provides methods for managing directories and their content.
DirAccess.swift:20class DirAccess
This class is used to manage directories and their content, even outside of the project folder.
DirAccess
can’t be instantiated directly. Instead it is created with a static method that takes a path for which it will be opened.
Most of the methods have a static alternative that can be used without creating a DirAccess
. Static methods only support absolute paths (including res://
and user://
).
Here is an example on how to iterate through the files of a directory:
Superclasses
class RefCounted
Base class for reference-counted objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
static func copyAbsolute(from: String, to: String, chmodFlags: Int32
) -> GodotError Static version of
copy(from:to:chmodFlags:)
. Supports only absolute paths.static func dirExistsAbsolute(path: String
) -> Bool Static version of
dirExists(path:)
. Supports only absolute paths.static func getDirectoriesAt(path: String
) -> PackedStringArray Returns a
PackedStringArray
containing filenames of the directory contents, excluding files, at the givenpath
. The array is sorted alphabetically.static func getDriveCount(
) -> Int32 On Windows, returns the number of drives (partitions) mounted on the current filesystem.
static func getDriveName(idx: Int32
) -> String On Windows, returns the name of the drive (partition) passed as an argument (e.g.
C:
).static func getFilesAt(path: String
) -> PackedStringArray Returns a
PackedStringArray
containing filenames of the directory contents, excluding directories, at the givenpath
. The array is sorted alphabetically.static func getOpenError(
) -> GodotError Returns the result of the last
open(path:)
call in the current thread.static func makeDirAbsolute(path: String
) -> GodotError Static version of
makeDir(path:)
. Supports only absolute paths.static func makeDirRecursiveAbsolute(path: String
) -> GodotError Static version of
makeDirRecursive(path:)
. Supports only absolute paths.static func open(path: String
) -> DirAccess? Creates a new
DirAccess
object and opens an existing directory of the filesystem. Thepath
argument can be within the project tree (res://folder
), the user directory (user://folder
) or an absolute path of the user filesystem (e.g./tmp/folder
orC:\tmp\folder
).static func removeAbsolute(path: String
) -> GodotError Static version of
remove(path:)
. Supports only absolute paths.static func renameAbsolute(from: String, to: String
) -> GodotError Static version of
rename(from:to:)
. Supports only absolute paths.class var godotClassName: StringName
Instance members
var includeHidden: Bool
If
true
, hidden files are included when navigating the directory.func changeDir(toDir: String
) -> GodotError Changes the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g.
newdir
or../newdir
), or an absolute path (e.g./tmp/newdir
orres://somedir/newdir
).func copy(from: String, to: String, chmodFlags: Int32
) -> GodotError Copies the
from
file to theto
destination. Both arguments should be paths to files, either relative or absolute. If the destination file exists and is not access-protected, it will be overwritten.func currentIsDir(
) -> Bool Returns whether the current item processed with the last
getNext
call is a directory (.
and..
are considered directories).func dirExists(path: String
) -> Bool Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.
func fileExists(path: String
) -> Bool Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.
func getCurrentDir(includeDrive: Bool
) -> String Returns the absolute path to the currently opened directory (e.g.
res://folder
orC:\tmp\folder
).func getCurrentDrive(
) -> Int32 Returns the currently opened directory’s drive index. See
getDriveName(idx:)
to convert returned index to the name of the drive.func getDirectories(
) -> PackedStringArray Returns a
PackedStringArray
containing filenames of the directory contents, excluding files. The array is sorted alphabetically.func getFiles(
) -> PackedStringArray Returns a
PackedStringArray
containing filenames of the directory contents, excluding directories. The array is sorted alphabetically.func getNext(
) -> String Returns the next element (file or directory) in the current directory.
func getSpaceLeft(
) -> UInt Returns the available space on the current directory’s disk, in bytes. Returns
0
if the platform-specific method to query the available space fails.func isCaseSensitive(path: String
) -> Bool Returns
true
if the file system or directory use case sensitive file names.func listDirBegin(
) -> GodotError Initializes the stream used to list all files and directories using the
getNext
function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed withlistDirEnd
.func listDirEnd(
) Closes the current stream opened with
listDirBegin
(whether it has been fully processed withgetNext
does not matter).func makeDir(path: String
) -> GodotError Creates a directory. The argument can be relative to the current directory, or an absolute path. The target directory should be placed in an already existing directory (to create the full path recursively, see
makeDirRecursive(path:)
).func makeDirRecursive(path: String
) -> GodotError Creates a target directory and all necessary intermediate directories in its path, by calling
makeDir(path:)
recursively. The argument can be relative to the current directory, or an absolute path.func remove(path: String
) -> GodotError Permanently deletes the target file or an empty directory. The argument can be relative to the current directory, or an absolute path. If the target directory is not empty, the operation will fail.
func rename(from: String, to: String
) -> GodotError Renames (move) the
from
file or directory to theto
destination. Both arguments should be paths to files or directories, either relative or absolute. If the destination file or directory exists and is not access-protected, it will be overwritten.