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], it must be explicitly opened using [method open] before most methods can be used. However, [method file_exists] and [method dir_exists] can be used without opening a directory. If so, they use a path relative to [code]res://[/code].
[b]Note:[/b] 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:
[codeblocks]
[gdscript]
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.")
[/gdscript]
[csharp]
public void DirContents(string path)
{
var dir = new Directory();
if (dir.Open(path) == Error.Ok)
{
dir.ListDirBegin();
string fileName = dir.GetNext();
while (fileName != "")
{
if (dir.CurrentIsDir())
{
GD.Print("Found directory: " + fileName);
}
else
{
GD.Print("Found file: " + fileName);
}
fileName = dir.GetNext();
}
}
else
{
GD.Print("An error occurred when trying to access the path.");
}
}
[/csharp]
[/codeblocks]
$DOCS_URL/tutorials/scripting/filesystem.html
Changes the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g. [code]newdir[/code] or [code]../newdir[/code]), or an absolute path (e.g. [code]/tmp/newdir[/code] or [code]res://somedir/newdir[/code]).
Returns one of the [enum Error] code constants ([code]OK[/code] on success).
Copies the [code]from[/code] file to the [code]to[/code] 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 [enum Error] code constants ([code]OK[/code] on success).
Returns whether the current item processed with the last [method get_next] call is a directory ([code].[/code] and [code]..[/code] are considered directories).
Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.
If the [Directory] is not open, the path is relative to [code]res://[/code].
Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.
If the [Directory] is not open, the path is relative to [code]res://[/code].
Returns the absolute path to the currently opened directory (e.g. [code]res://folder[/code] or [code]C:\tmp\folder[/code]).
Returns the currently opened directory's drive index. See [method get_drive] to convert returned index to the name of the drive.
Returns a [PackedStringArray] containing filenames of the directory contents, excluding files. The array is sorted alphabetically.
Affected by [member include_hidden] and [member include_navigational].
On Windows, returns the name of the drive (partition) passed as an argument (e.g. [code]C:[/code]). On other platforms, or if the requested drive does not exist, the method returns an empty String.
On Windows, returns the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0.
Returns a [PackedStringArray] containing filenames of the directory contents, excluding directories. The array is sorted alphabetically.
Affected by [member include_hidden].
Returns the next element (file or directory) in the current directory (including [code].[/code] and [code]..[/code], unless [code]skip_navigational[/code] was given to [method list_dir_begin]).
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. [method list_dir_end] would not be mandatory in such a case).
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.
Initializes the stream used to list all files and directories using the [method get_next] function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with [method list_dir_end].
Affected by [member include_hidden] and [member include_navigational].
[b]Note:[/b] The order of files and directories returned by this method is not deterministic, and can vary between operating systems. If you want a list of all files or folders sorted alphabetically, use [method get_files] or [method get_directories].
Closes the current stream opened with [method list_dir_begin] (whether it has been fully processed with [method get_next] does not matter).
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 [method make_dir_recursive]).
Returns one of the [enum Error] code constants ([code]OK[/code] on success).
Creates a target directory and all necessary intermediate directories in its path, by calling [method make_dir] recursively. The argument can be relative to the current directory, or an absolute path.
Returns one of the [enum Error] code constants ([code]OK[/code] on success).
Opens an existing directory of the filesystem. The [code]path[/code] argument can be within the project tree ([code]res://folder[/code]), the user directory ([code]user://folder[/code]) or an absolute path of the user filesystem (e.g. [code]/tmp/folder[/code] or [code]C:\tmp\folder[/code]).
Returns one of the [enum Error] code constants ([code]OK[/code] on success).
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 [enum Error] code constants ([code]OK[/code] on success).
Renames (move) the [code]from[/code] file or directory to the [code]to[/code] 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 [enum Error] code constants ([code]OK[/code] on success).
If [code]true[/code], hidden files are included when the navigating directory.
Affects [method list_dir_begin], [method get_directories] and [method get_files].
If [code]true[/code], [code].[/code] and [code]..[/code] are included when navigating the directory.
Affects [method list_dir_begin] and [method get_directories].