public class Directory : Reference
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:
var dir = Directory.new()
if dir.open(path) == OK:
var file_name = dir.get_next()
while (file_name != “”):
print(“Found directory: “ + file_name)
print(“Found file: “ + file_name)
file_name = dir.get_next()
print(“An error occurred when trying to access the path.”)
public Error ChangeDir(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 an absolute path (e.g.
public Error Copy(string from, 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.
public bool CurrentIsDir()
Return whether the current item processed with the last GetNext() call is a directory (
..are considered directories).
public bool DirExists(string path)
public bool FileExists(string path)
public string GetCurrentDir()
public int GetCurrentDrive()
Returns the currently opened directory’s drive index. See GetDrive(int) to convert returned index to the name of the drive.
public string GetDrive(int idx)
public int GetDriveCount()
public string GetNext()
Return the next element (file or directory) in the current directory (including
skip_navigationalwas given to ListDirBegin(bool, bool)).
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. ListDirEnd() would not be mandatory in such a case).
public int GetSpaceLeft()
public Error ListDirBegin(bool skipNavigational = false, bool skipHidden = false)
Initialise the stream used to list all files and directories using the GetNext() function, closing the current opened stream if needed. Once the stream has been processed, it should typically be closed with ListDirEnd().
If you pass
..would be filtered out.
public void ListDirEnd()
public Error MakeDir(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 MakeDirRecursive(string)).
public Error MakeDirRecursive(string path)
Create a target directory and all necessary intermediate directories in its path, by calling MakeDir(string) recursively. The argument can be relative to the current directory, or an absolute path.
public Error Open(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.
public Error Remove(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.
public Error Rename(string from, 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.
Return one of the error code constants defined in [@GlobalScope] (OK or FAILED).