.. Generated automatically by doc/tools/makerst.py in Godot's source tree. .. DO NOT EDIT THIS FILE, but the doc/base/classes.xml source instead. .. _class_Directory: Directory ========= **Inherits:** :ref:`Reference` **<** :ref:`Object` **Category:** Core Brief Description ----------------- Type used to handle the filesystem. Member Functions ---------------- +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`open` **(** :ref:`String` path **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`list_dir_begin` **(** **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_next` **(** **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`current_is_dir` **(** **)** const | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | void | :ref:`list_dir_end` **(** **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_drive_count` **(** **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_drive` **(** :ref:`int` idx **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`change_dir` **(** :ref:`String` todir **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`String` | :ref:`get_current_dir` **(** **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`make_dir` **(** :ref:`String` path **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`make_dir_recursive` **(** :ref:`String` path **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`file_exists` **(** :ref:`String` path **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`bool` | :ref:`dir_exists` **(** :ref:`String` path **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | :ref:`int` | :ref:`get_space_left` **(** **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`copy` **(** :ref:`String` from, :ref:`String` to **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`rename` **(** :ref:`String` from, :ref:`String` to **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ | Error | :ref:`remove` **(** :ref:`String` path **)** | +------------------------------+----------------------------------------------------------------------------------------------------------------------+ Description ----------- 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: :: 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.") Member Function Description --------------------------- .. _class_Directory_open: - Error **open** **(** :ref:`String` path **)** 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. ``/tmp/folder`` or ``C:\tmp\folder``). The method returns one of the error code constants defined in :ref:`@Global Scope` (OK or ERR\_\*). .. _class_Directory_list_dir_begin: - :ref:`bool` **list_dir_begin** **(** **)** Initialise the stream used to list all files and directories using the :ref:`get_next` function, closing the current opened stream if needed. Once the stream has been processed, it should typically be closed with :ref:`list_dir_end`. Return false if the stream could not be initialised. .. _class_Directory_get_next: - :ref:`String` **get_next** **(** **)** Return the next element (file or directory) in the current directory (including ``.`` and ``..``). 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. :ref:`list_dir_end` would not be mandatory in such a case). .. _class_Directory_current_is_dir: - :ref:`bool` **current_is_dir** **(** **)** const Return whether the current item processed with the last :ref:`get_next` call is a directory (``.`` and ``..`` are considered directories). .. _class_Directory_list_dir_end: - void **list_dir_end** **(** **)** Close the current stream opened with :ref:`list_dir_begin` (whether it has been fully processed with :ref:`get_next` or not does not matter). .. _class_Directory_get_drive_count: - :ref:`int` **get_drive_count** **(** **)** On Windows, return the number of drives (partitions) mounted on the current filesystem. On other platforms, the method returns 0. .. _class_Directory_get_drive: - :ref:`String` **get_drive** **(** :ref:`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. .. _class_Directory_change_dir: - Error **change_dir** **(** :ref:`String` todir **)** Change 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``). The method returns one of the error code constants defined in :ref:`@Global Scope` (OK or ERR\_\*). .. _class_Directory_get_current_dir: - :ref:`String` **get_current_dir** **(** **)** Return the absolute path to the currently opened directory (e.g. ``res://folder`` or ``C:\tmp\folder``). .. _class_Directory_make_dir: - Error **make_dir** **(** :ref:`String` path **)** 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 :ref:`make_dir_recursive`). The method returns one of the error code constants defined in :ref:`@Global Scope` (OK, FAILED or ERR\_\*). .. _class_Directory_make_dir_recursive: - Error **make_dir_recursive** **(** :ref:`String` path **)** Create a target directory and all necessary intermediate directories in its path, by calling :ref:`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 :ref:`@Global Scope` (OK, FAILED or ERR\_\*). .. _class_Directory_file_exists: - :ref:`bool` **file_exists** **(** :ref:`String` path **)** Return whether the target file exists. The argument can be relative to the current directory, or an absolute path. .. _class_Directory_dir_exists: - :ref:`bool` **dir_exists** **(** :ref:`String` path **)** Return whether the target directory exists. The argument can be relative to the current directory, or an absolute path. .. _class_Directory_get_space_left: - :ref:`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. .. _class_Directory_copy: - Error **copy** **(** :ref:`String` from, :ref:`String` to **)** 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 :ref:`@Global Scope` (OK, FAILED or ERR\_\*). .. _class_Directory_rename: - Error **rename** **(** :ref:`String` from, :ref:`String` to **)** 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 :ref:`@Global Scope` (OK or FAILED). .. _class_Directory_remove: - Error **remove** **(** :ref:`String` path **)** 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 :ref:`@Global Scope` (OK or FAILED).