Directory
Inherits: Reference < Object Type used to handle the filesystem.
Description
Directory type. It is used to manage directories and their content (not restricted to the project folder).
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:
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.")
Tutorials
- File system
Methods
Method Descriptions
- Error change_dir ( String todir )
newdiror../newdir), or an absolute path (e.g./tmp/newdirorres://somedir/newdir). Error code constants (OKon success).
- Error copy ( String from, String to )
fromfile to thetodestination. 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. Error code constants (OKon success).
- bool current_is_dir ( ) const
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.
- bool file_exists ( String path ) Returns whether the target file exists. The argument can be relative to the current directory, or an absolute path.
- String get_current_dir ( )
res://folderorC:\tmp\folder).
- int get_current_drive ( ) get_drive to convert returned index to the name of the drive.
- String get_drive ( int idx )
C:). On macOS, returns the path to the mounted volume passed as an argument. On Linux, returns the path to the mounted volume or GTK 3 bookmark passed as an argument. On other platforms, or if the requested drive does not exist, the method returns an empty String.
- int get_drive_count ( ) On Windows, returns the number of drives (partitions) mounted on the current filesystem. On macOS, returns the number of mounted volumes. On Linux, returns the number of mounted volumes and GTK 3 bookmarks. On other platforms, the method returns 0.
- String get_next ( )
.and.., unlessskip_navigationalwas given to list_dir_begin). list_dir_end would not be mandatory in such a case).
- int get_space_left ( ) 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.
- Error list_dir_begin ( bool skip_navigational=false, bool skip_hidden=false )
get_next function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with list_dir_end.
skip_navigationalistrue,.and..are filtered out.skip_hiddenistrue, hidden files are filtered out.
- list_dir_end ( ) list_dir_begin (whether it has been fully processed with get_next does not matter).
- Error make_dir ( String path )
make_dir_recursive).
Error code constants (
OKon success).
- Error make_dir_recursive ( String path )
make_dir recursively. The argument can be relative to the current directory, or an absolute path.
Error code constants (
OKon success).
- Error open ( String path )
pathargument can be within the project tree (res://folder), the user directory (user://folder) or an absolute path of the user filesystem (e.g./tmp/folderorC:\tmp\folder). Error code constants (OKon success).
- Error remove ( String path )
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.
OS.move_to_trash instead.
Error code constants (
OKon success).
- Error rename ( String from, String to )
fromfile or directory to thetodestination. 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. Error code constants (OKon success).
