Directory

    Category: Core

    Type used to handle the filesystem.

    Directory type. It is used to manage directories and their content (not restricted to the project folder).

    Here is an example on how to iterate through the files of a directory:

    • Error change_dir ( todir )

    Change the currently opened directory to the one passed as an argument. The argument can be relative to the current directory (e.g. or ../newdir), or an absolute path (e.g. /tmp/newdir or res://somedir/newdir).

    The method returns one of the error code constants defined in @GlobalScope (OK or ERR_*).


    Copy 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 defined in (OK, FAILED or ERR_*).


    • bool current_is_dir ( ) const

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


    • bool dir_exists ( path )

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


    • bool file_exists ( path )

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


    • get_current_drive ( )

    Returns the currently opened directory’s drive index. See get_drive to convert returned index to the name of the drive.


    • get_drive ( int idx )

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


    • get_drive_count ( )

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


    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. would not be mandatory in such a case).


    • int get_space_left ( )

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


    • list_dir_begin ( bool skip_navigational=false, skip_hidden=false )

    Initialize the stream used to list all files and directories using the get_next function, closing the current opened stream if needed. Once the stream has been processed, it should typically be closed with .

    If you pass , then . and .. would be filtered out.

    If you pass skip_hidden, then hidden files would be filtered out.


    • void list_dir_end ( )

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

    The method returns one of the error code constants defined in (OK, FAILED or ERR_*).


    • Error make_dir_recursive ( path )

    Create 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 defined in (OK, FAILED or ERR_*).


    Open 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. or C:\tmp\folder).

    The method returns one of the error code constants defined in @GlobalScope (OK or ERR_*).


    Delete 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 defined in (OK or FAILED).


    Rename (move) 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 defined in @GlobalScope (OK or FAILED).