DirAccess

继承: RefCounted < Object

Provides methods for managing directories and their content.

描述

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.");
        }
    }

属性

方法


属性说明

bool include_hidden

  • void set_include_hidden ( value: bool )
  • bool get_include_hidden ( )

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

Affects list_dir_begin, get_directories and get_files.


bool include_navigational

  • void set_include_navigational ( value: bool )
  • bool get_include_navigational ( )

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

Affects list_dir_begin and get_directories.


方法说明

Error change_dir ( to_dir: String )

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 ( from: String, to: String, chmod_flags: int = -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 ( from: String, to: String, chmod_flags: int = -1 ) static1

Static version of copy. Supports only absolute paths.


Error create_link ( source: String, target: String )

Creates symbolic link between files or folders.

Note: On Windows, this method works only if the application is running with elevated privileges or Developer Mode is enabled.

Note: This method is implemented on macOS, Linux, and Windows.


bool current_is_dir ( ) const2

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


bool dir_exists ( path: String )

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


bool dir_exists_absolute ( path: String ) static1

Static version of dir_exists. Supports only absolute paths.


bool file_exists ( path: String )

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

For a static equivalent, use FileAccess.file_exists.


String get_current_dir ( include_drive: bool = true ) const2

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_name to convert returned index to the name of the drive.


PackedStringArray get_directories ( )

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

Affected by include_hidden and include_navigational.


PackedStringArray get_directories_at ( path: String ) static1

Returns a PackedStringArray containing filenames of the directory contents, excluding files, at the given path. The array is sorted alphabetically.

Use get_directories if you want more control of what gets included.


int get_drive_count ( ) static1

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_drive_name ( idx: int ) static1

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.


PackedStringArray get_files ( )

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

Affected by include_hidden.

Note: When used on a res:// path in an exported project, only the files actually included in the PCK at the given folder level are returned. In practice, this means that since imported resources are stored in a top-level .godot/ folder, only paths to *.gd and *.import files are returned (plus a few files such as project.godot or project.binary and the project icon). In an exported project, the list of returned files will also vary depending on whether ProjectSettings.editor/export/convert_text_resources_to_binary is true.


PackedStringArray get_files_at ( path: String ) static1

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

Use get_files if you want more control of what gets included.


String get_next ( )

Returns the next element (file or directory) in the current directory.

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


Error get_open_error ( ) static1

Returns the result of the last open call in the current thread.


int get_space_left ( )

Returns the available space on the current directory's disk, in bytes. Returns 0 if the platform-specific method to query the available space fails.


bool is_case_sensitive ( path: String ) const2

Returns true if the file system or directory use case sensitive file names.

Note: This method is implemented on macOS, Linux (for EXT4 and F2FS filesystems only) and Windows. On other platforms, it always returns true.


bool is_link ( path: String )

Returns true if the file or directory is a symbolic link, directory junction, or other reparse point.

Note: This method is implemented on macOS, Linux, and Windows.


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


Error make_dir ( path: String )

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 (@GlobalScope.OK on success).


Error make_dir_absolute ( path: String ) static1

Static version of make_dir. Supports only absolute paths.


Error make_dir_recursive ( path: String )

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 (@GlobalScope.OK on success).


Error make_dir_recursive_absolute ( path: String ) static1

Static version of make_dir_recursive. Supports only absolute paths.


DirAccess open ( path: String ) static1

Creates a new DirAccess object and 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 null if opening the directory failed. You can use get_open_error to check the error that occurred.


String read_link ( path: String )

Returns target of the symbolic link.

Note: This method is implemented on macOS, Linux, and Windows.


Error remove ( path: String )

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 (@GlobalScope.OK on success).


Error remove_absolute ( path: String ) static1

Static version of remove. Supports only absolute paths.


Error rename ( from: String, to: String )

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 (@GlobalScope.OK on success).


Error rename_absolute ( from: String, to: String ) static1

Static version of rename. Supports only absolute paths.

3

本方法通常需要用户覆盖才能生效。

2

本方法无副作用,不会修改该实例的任何成员变量。

4

本方法除了能接受在此处描述的参数外,还能够继续接受任意数量的参数。

5

本方法用于构造某个类型。

1

调用本方法无需实例,可直接使用类名进行调用。

6

本方法描述的是使用本类型作为左操作数的有效运算符。

7

这个值是由下列位标志构成位掩码的整数。

8

无返回值。