DirAccess

package
v0.0.0-...-e1beaa7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

This class is used to manage directories and their content, even outside of the project folder.

graphics.gd/classdb/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 graphics.gd/classdb/DirAccess. Static methods only support absolute paths (including res:// and user://).

package main

import "graphics.gd/classdb/DirAccess"

func ExampleDirectoryMake() {
	var dir = DirAccess.Open("user://levels")
	dir.MakeDir("world1")
	DirAccess.MakeDirAbsolute("user://levels/world1")
}

Note: Accessing project ("res://") directories once exported may behave unexpectedly as some files are converted to engine-specific formats and their original source files may not be present in the expected PCK package. Because of this, to access resources in an exported project, it is recommended to use graphics.gd/classdb/ResourceLoader instead of graphics.gd/classdb/FileAccess.

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

package main

import "graphics.gd/classdb/DirAccess"

func ExampleDirectoryList(path string) {
	var dir = DirAccess.Open(path)
	if dir != DirAccess.Nil {
		dir.ListDirBegin()
		var fileName = dir.GetNext()
		for fileName != "" {
			if dir.CurrentIsDir() {
				println("Found directory: " + fileName)
			} else {
				println("Found file: " + fileName)
			}
			fileName = dir.GetNext()
		}
	} else {
		println("An error occurred when trying to access the path.")
	}
}

Keep in mind that file names may change or be remapped after export. If you want to see the actual resource file list as it appears in the editor, use graphics.gd/classdb/ResourceLoader.ListDirectory instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyAbsolute

func CopyAbsolute(from string, to string) error

Static version of Instance.Copy. Supports only absolute paths.

func CopyAbsoluteOptions

func CopyAbsoluteOptions(from string, to string, chmod_flags int) error

Static version of Instance.Copy. Supports only absolute paths.

func DirExistsAbsolute

func DirExistsAbsolute(path string) bool

Static version of Instance.DirExists. Supports only absolute paths.

Note: The returned bool in the editor and after exporting when used on a path in the res:// directory may be different. Some files are converted to engine-specific formats when exported, potentially changing the directory structure.

func GetDirectoriesAt

func GetDirectoriesAt(path string) []string

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

Use Instance.GetDirectories if you want more control of what gets included.

Note: The returned directories in the editor and after exporting in the res:// directory may differ as some files are converted to engine-specific formats when exported.

func GetDriveCount

func GetDriveCount() int

On Windows, returns the number of drives (partitions) mounted on the current filesystem.

On macOS and Android, 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.

func GetDriveName

func GetDriveName(idx int) string

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 Android (API level 30+), returns the path to the mounted volume as an argument.

On other platforms, or if the requested drive does not exist, the method returns an empty String.

func GetFilesAt

func GetFilesAt(path string) []string

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

Use Instance.GetFiles if you want more control of what gets included.

Note: When used on a res:// path in an exported project, only the files 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 other 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 graphics.gd/classdb/ProjectSettings "editor/export/convert_text_resources_to_binary".

func GetOpenError

func GetOpenError() error

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

func MakeDirAbsolute

func MakeDirAbsolute(path string) error

Static version of Instance.MakeDir. Supports only absolute paths.

func MakeDirRecursiveAbsolute

func MakeDirRecursiveAbsolute(path string) error

Static version of Instance.MakeDirRecursive. Supports only absolute paths.

func RemoveAbsolute

func RemoveAbsolute(path string) error

Static version of Instance.Remove. Supports only absolute paths.

func RenameAbsolute

func RenameAbsolute(from string, to string) error

Static version of Instance.Rename. Supports only absolute paths.

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsDirAccess() Instance
}

type Expanded

type Expanded [1]gdclass.DirAccess

func (Expanded) Copy

func (self Expanded) Copy(from string, to string, chmod_flags int) error

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 ([Ok] on success).

func (Expanded) GetCurrentDir

func (self Expanded) GetCurrentDir(include_drive bool) string

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

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsDirAccess

func (self *Extension[T]) AsDirAccess() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.DirAccess

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func CreateTemp

func CreateTemp(prefix string, keep bool) Instance

Creates a temporary directory. This directory will be freed when the returned graphics.gd/classdb/DirAccess is freed.

If 'prefix' is not empty, it will be prefixed to the directory name, separated by a -.

If 'keep' is true, the directory is not deleted when the returned graphics.gd/classdb/DirAccess is freed.

Returns null if opening the directory failed. You can use GetOpenError to check the error that occurred.

func CreateTempOptions

func CreateTempOptions(prefix string, keep bool) Instance

Creates a temporary directory. This directory will be freed when the returned graphics.gd/classdb/DirAccess is freed.

If 'prefix' is not empty, it will be prefixed to the directory name, separated by a -.

If 'keep' is true, the directory is not deleted when the returned graphics.gd/classdb/DirAccess is freed.

Returns null if opening the directory failed. You can use GetOpenError to check the error that occurred.

func New

func New() Instance

func Open

func Open(path string) Instance

Creates a new graphics.gd/classdb/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 GetOpenError to check the error that occurred.

func (Instance) AsDirAccess

func (self Instance) AsDirAccess() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) ChangeDir

func (self Instance) ChangeDir(to_dir string) error

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

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.

func (Instance) Copy

func (self Instance) Copy(from string, to string) error

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 ([Ok] on success).

func (self Instance) CreateLink(source string, target string) error

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.

func (Instance) CurrentIsDir

func (self Instance) CurrentIsDir() bool

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

func (Instance) DirExists

func (self Instance) DirExists(path string) bool

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

Note: The returned bool in the editor and after exporting when used on a path in the res:// directory may be different. Some files are converted to engine-specific formats when exported, potentially changing the directory structure.

func (Instance) FileExists

func (self Instance) FileExists(path string) bool

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

For a static equivalent, use graphics.gd/classdb/FileAccess.Instance.FileExists.

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. See graphics.gd/classdb/ResourceLoader.Exists for an alternative approach that takes resource remapping into account.

func (Instance) GetCurrentDir

func (self Instance) GetCurrentDir() string

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

func (Instance) GetCurrentDrive

func (self Instance) GetCurrentDrive() int

Returns the currently opened directory's drive index. See GetDriveName to convert returned index to the name of the drive.

func (Instance) GetDirectories

func (self Instance) GetDirectories() []string

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

Affected by Instance.IncludeHidden and Instance.IncludeNavigational.

Note: The returned directories in the editor and after exporting in the res:// directory may differ as some files are converted to engine-specific formats when exported.

func (Instance) GetFiles

func (self Instance) GetFiles() []string

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

Affected by Instance.IncludeHidden.

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 graphics.gd/classdb/ProjectSettings "editor/export/convert_text_resources_to_binary" is true.

func (Instance) GetFilesystemType

func (self Instance) GetFilesystemType() string

Returns file system type name of the current directory's disk. Returned values are uppercase strings like NTFS, FAT32, EXFAT, APFS, EXT4, BTRFS, and so on.

Note: This method is implemented on macOS, Linux, Windows and for PCK virtual file system.

func (Instance) GetNext

func (self Instance) GetNext() string

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

func (Instance) GetSpaceLeft

func (self Instance) GetSpaceLeft() int

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.

func (Instance) ID

func (self Instance) ID() ID

func (Instance) IncludeHidden

func (self Instance) IncludeHidden() bool

func (Instance) IncludeNavigational

func (self Instance) IncludeNavigational() bool

func (Instance) IsBundle

func (self Instance) IsBundle(path string) bool

Returns true if the directory is a macOS bundle.

Note: This method is implemented on macOS.

func (Instance) IsCaseSensitive

func (self Instance) IsCaseSensitive(path string) bool

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.

func (Instance) IsEquivalent

func (self Instance) IsEquivalent(path_a string, path_b string) bool

Returns true if paths 'path_a' and 'path_b' resolve to the same file system object. Returns false otherwise, even if the files are bit-for-bit identical (e.g., identical copies of the file that are not symbolic links).

func (self Instance) IsLink(path string) bool

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.

func (Instance) Iter

func (dir Instance) Iter() iter.Seq[string]

Iter returns an iterator over the items of the [DirAccess].

func (Instance) ListDirBegin

func (self Instance) ListDirBegin() error

Initializes the stream used to list all files and directories using the Instance.GetNext function, closing the currently opened stream if needed. Once the stream has been processed, it should typically be closed with Instance.ListDirEnd.

Affected by Instance.IncludeHidden and Instance.IncludeNavigational.

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 Instance.GetFiles or Instance.GetDirectories.

func (Instance) ListDirEnd

func (self Instance) ListDirEnd()

Closes the current stream opened with Instance.ListDirBegin (whether it has been fully processed with Instance.GetNext does not matter).

func (Instance) MakeDir

func (self Instance) MakeDir(path string) error

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

Returns one of the [Error] code constants ([Ok] on success).

func (Instance) MakeDirRecursive

func (self Instance) MakeDirRecursive(path string) error

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

Returns one of the [Error] code constants ([Ok] on success).

func (self Instance) ReadLink(path string) string

Returns target of the symbolic link.

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

func (Instance) Remove

func (self Instance) Remove(path string) error

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 graphics.gd/classdb/OS.MoveToTrash instead.

Returns one of the [Error] code constants ([Ok] on success).

func (Instance) Rename

func (self Instance) Rename(from string, to string) error

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

func (Instance) SetIncludeHidden

func (self Instance) SetIncludeHidden(value bool)

func (Instance) SetIncludeNavigational

func (self Instance) SetIncludeNavigational(value bool)

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL