Up to date
This page is up to date for Godot 4.2
.
If you still find outdated information, please open an issue.
DirAccess¶
Inherits: RefCounted < Object
Provides methods for managing directories and their content.
Description¶
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://
).
# Standard
var dir = DirAccess.open("user://levels")
dir.make_dir("world1")
# Static
DirAccess.make_dir_absolute("user://levels/world1")
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:
func dir_contents(path):
var dir = DirAccess.open(path)
if dir:
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.")
public void DirContents(string path)
{
using var dir = DirAccess.Open(path);
if (dir != null)
{
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.");
}
}
Tutorials¶
Properties¶
Methods¶
change_dir ( String to_dir ) |
|
copy_absolute ( String from, String to, int chmod_flags=-1 ) static |
|
current_is_dir ( ) const |
|
dir_exists ( String path ) |
|
dir_exists_absolute ( String path ) static |
|
file_exists ( String path ) |
|
get_current_dir ( bool include_drive=true ) const |
|
get_directories ( ) |
|
get_directories_at ( String path ) static |
|
get_drive_count ( ) static |
|
get_drive_name ( int idx ) static |
|
get_files ( ) |
|
get_files_at ( String path ) static |
|
get_next ( ) |
|
get_open_error ( ) static |
|
get_space_left ( ) |
|
is_case_sensitive ( String path ) const |
|
list_dir_begin ( ) |
|
void |
list_dir_end ( ) |
make_dir_absolute ( String path ) static |
|
make_dir_recursive ( String path ) |
|
make_dir_recursive_absolute ( String path ) static |
|
remove_absolute ( String path ) static |
|
rename_absolute ( String from, String to ) static |
Property Descriptions¶
If true
, hidden files are included when navigating the directory.
Affects list_dir_begin, get_directories and get_files.
If true
, .
and ..
are included when navigating the directory.
Affects list_dir_begin and get_directories.
Method Descriptions¶
Error change_dir ( String to_dir )
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 (@GlobalScope.OK on success).
Note: The new directory must be within the same scope, e.g. when you had opened a directory inside res://
, you can't change it to user://
directory. If you need to open a directory in another access scope, use open to create a new instance instead.
Error copy ( String from, String to, int chmod_flags=-1 )
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.
If chmod_flags
is different than -1
, the Unix permissions for the destination path will be set to the provided value, if available on the current operating system.
Returns one of the Error code constants (@GlobalScope.OK on success).
Error copy_absolute ( String from, String to, int chmod_flags=-1 ) static
Static version of copy. Supports only absolute paths.
bool current_is_dir ( ) const
Returns whether the current item processed with the last get_next call is a directory (.
and ..
are considered directories).
bool dir_exists ( String path )
Returns whether the target directory exists. The argument can be relative to the current directory, or an absolute path.