Directory

Type used to handle the filesystem.

Directory type. It is used to manage directories and their content (not restricted to the project folder). When creating a new Directory, its default opened directory will be res://. This may change in the future, so it is advised to always use open to initialize your Directory where you want to operate, with explicit error checking. Note: Many resources types are imported (e.g. textures or sound files), and their source asset will not be included in the exported game, as only the imported version is used. Use ResourceLoader to access imported resources. Here is an example on how to iterate through the files of a directory:

More...

Members

Aliases

BaseClasses
alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses)
Undocumented in source.

Functions

changeDir
GodotError changeDir(String todir)

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 or res://somedir/newdir). Returns one of the error code constants (OK on success).

copy
GodotError copy(String from, String to)

Copies the from file to the to 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. Returns one of the error code constants (OK on success).

currentIsDir
bool currentIsDir()

Returns whether the current item processed with the last getNext call is a directory (. and .. are considered directories).

dirExists
bool dirExists(String path)

Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.

fileExists
bool fileExists(String path)

Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.

getCurrentDir
String getCurrentDir()

Returns the absolute path to the currently opened directory (e.g. res://folder or C:\tmp\folder).

getCurrentDrive
long getCurrentDrive()

Returns the currently opened directory's drive index. See getDrive to convert returned index to the name of the drive.

getDrive
String getDrive(long idx)

On Windows, returns the name of the drive (partition) passed as an argument (e.g. C:). On other platforms, or if the requested drive does not exist, the method returns an empty String.

getDriveCount
long getDriveCount()

On Windows, returns the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0.

getNext
String getNext()

Returns the next element (file or directory) in the current directory (including . and .., unless skip_navigational was given to listDirBegin). The name of the file or directory is returned (and not its full path). Once the stream has been fully processed, the method returns an empty String and closes the stream automatically (i.e. listDirEnd would not be mandatory in such a case).

getSpaceLeft
long getSpaceLeft()

On UNIX desktop systems, returns the available space on the current directory's disk. On other platforms, this information is not available and the method returns 0 or -1.

listDirBegin
GodotError listDirBegin(bool skip_navigational, bool skip_hidden)

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 with listDirEnd. If skip_navigational is true, . and .. are filtered out. If skip_hidden is true, hidden files are filtered out.

listDirEnd
void listDirEnd()

Closes the current stream opened with listDirBegin (whether it has been fully processed with getNext does not matter).

makeDir
GodotError makeDir(String path)

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). Returns one of the error code constants (OK on success).

makeDirRecursive
GodotError makeDirRecursive(String path)

Creates a target directory and all necessary intermediate directories in its path, by calling makeDir recursively. The argument can be relative to the current directory, or an absolute path. Returns one of the error code constants (OK on success).

opAssign
typeof(null) opAssign(typeof(null) n)
opEquals
bool opEquals(Directory other)
opEquals
bool opEquals(typeof(null) n)
open
GodotError open(String path)

Opens an existing directory of the filesystem. The path 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 or C:\tmp\folder). Returns one of the error code constants (OK on success).

remove
GodotError remove(String path)

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. Returns one of the error code constants (OK on success).

rename
GodotError rename(String from, String to)

Renames (move) the from file or directory to the to 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. Returns one of the error code constants (OK on success).

toHash
size_t toHash()

Mixins

__anonymous
mixin baseCasts
Undocumented in source.

Static functions

_new
Directory _new()

Construct a new instance of Directory. Note: use memnew!Directory instead.

Static variables

_classBindingInitialized
bool _classBindingInitialized;
Undocumented in source.

Structs

GDNativeClassBinding
struct GDNativeClassBinding
Undocumented in source.

Unions

__anonymous
union __anonymous
Undocumented in source.

Variables

_GODOT_internal_name
enum string _GODOT_internal_name;
Undocumented in source.

Mixed In Members

From mixin baseCasts

as
inout(To) as()
Undocumented in source. Be warned that the author may not have intended to support it.
as
inout(To) as()
Undocumented in source. Be warned that the author may not have intended to support it.
as
inout(ToRef) as()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
template opCast(To)
Undocumented in source.
opCast
template opCast(To)
Undocumented in source.
opCast
template opCast(ToRef)
Undocumented in source.
opCast
void* opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
godot_object opCast()
Undocumented in source. Be warned that the author may not have intended to support it.
opCast
bool opCast()
Undocumented in source. Be warned that the author may not have intended to support it.

Detailed Description

func dir_contents(path): var dir = Directory.new() if dir.open(path) == OK: dir.list_dir_begin() var file_name = dir.get_next() while file_name != "": if dir.current_is_dir(): print("Found directory: " + file_name) else: print("Found file: " + file_name) file_name = dir.get_next() else: print("An error occurred when trying to access the path.")

Meta