Directory

Inherits: RefCounted < 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).

When creating a new Directory, it must be explicitly opened using open before most methods can be used. However, file_exists and dir_exists can be used without opening a directory. If so, they use a path relative to res://.

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

Properties

bool

include_hidden

false

bool

include_navigational

false

Methods

Error

change_dir ( String todir )

Error

copy ( String from, String to )

bool

current_is_dir ( ) const

bool

dir_exists ( String path )

bool

file_exists ( String path )

String

get_current_dir ( )

int

get_current_drive ( )

PackedStringArray

get_directories ( )

String

get_drive ( int idx )

int

get_drive_count ( )

PackedStringArray

get_files ( )

String

get_next ( )

int

get_space_left ( )

Error

list_dir_begin ( )

void

list_dir_end ( )

Error

make_dir ( String path )

Error

make_dir_recursive ( String path )

Error

open ( String path )

Error

remove ( String path )

Error

rename ( String from, String to )

Property Descriptions

  • bool include_hidden

Default

false

Setter

set_include_hidden(value)

Getter

get_include_hidden()

If true, hidden files are included when the navigating directory.

Affects list_dir_begin, get_directories and get_files.


  • bool include_navigational

Default

false

Setter

set_include_navigational(value)

Getter

get_include_navigational()

If true, . and .. are included when navigating the directory.

Affects list_dir_begin and get_directories.

Method Descriptions

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).


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).


  • bool current_is_dir ( ) const

Returns whether the current item processed with the last get_next call is a directory (. and .. 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 res://.


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 res://.


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


  • int get_current_drive ( )

Returns the currently opened directory's drive index. See 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 include_hidden and include_navigational.


On Windows, returns the name of the drive (partition) passed as an argument (e.g. 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.


Returns a PackedStringArray containing filenames of the directory contents, excluding directories. The array is sorted alphabetically.

Affected by include_hidden.


Returns the next element (file or directory) in the current directory (including . and .., unless skip_navigational was given to 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. 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 ( )

Initializes the stream used to list all files and directories using the 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.

Affected by include_hidden and include_navigational.

Note: 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 get_files or get_directories.


  • void list_dir_end ( )

Closes the current stream opened with list_dir_begin (whether it has been fully processed with 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 make_dir_recursive).

Returns one of the Error code constants (OK on success).


Creates a target directory and all necessary intermediate directories in its path, by calling make_dir recursively. The argument can be relative to the current directory, or an absolute path.

Returns one of the Error code constants (OK on success).


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).


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.

If you don't want to delete the file/directory permanently, use OS.move_to_trash instead.

Returns one of the Error code constants (OK on success).


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).