dreamworkAPI
Ctrl K
GitHub
classDocs / Directory

Directory

classsharedmenusource
Object → Directory

The directory object.

A thin handle over a vfs path, every call goes to vfs, so the object can exist before its directory.

Moving and renaming are asynchronous inside of a coroutine and synchronous outside of it.

lua
local directory = Directory( "/garrysmod/data/test", true )
local files, file_count = directory:search( "*.txt" )

Constructor

Directory(directory_path: string, touch: boolean?): Directory

Fields

NameTypeDescription
pathstringThe absolute virtual path of the directory, assigning moves the directory without overwriting, the target directory must exist.
namestringThe name of the directory, empty for the root, assigning renames the directory without overwriting.
sizeintegerThe total size of everything in the directory in bytes, walked recursively, 0 if the directory does not exist. Read-only.
parentDirectoryThe parent directory, the root returns itself, assigning moves the directory into the directory without overwriting.

Static functions

existsChecks if a directory exists.
searchLists files and directories in a directory by wildcard, subdirectories are not searched.

#Directory.exists

sharedmenunodiscardsource
function Directory.exists(directory_path: string): boolean

Checks if a directory exists.

Parameters
NameTypeDescription
directory_pathstringThe absolute path to the directory.
Returns
TypeNameDescription
booleanexiststrue if the directory exists, false otherwise.

#Directory.search

sharedmenunodiscardsource
function Directory.search(directory_path: string, wildcard?: string): File[], integer, Directory[], integer

Lists files and directories in a directory by wildcard, subdirectories are not searched.

lua
local files, file_count, directories, directory_count = Directory.search( "/garrysmod/data", "*.txt" )
Parameters
NameTypeDescription
directory_pathstringThe absolute path to the directory.
wildcardoptionalstringThe wildcard names must match, see path.wildcard, * by default.
Returns
TypeNameDescription
File[]filesThe found files.
integerfile_countThe number of the found files.
Directory[]directoriesThe found directories.
integerdirectory_countThe number of the found directories.

Methods

deleteDeletes the directory, the object stays usable and touch creates it again.
directoriesReturns the iterator over the directories inside of the directory.
existsChecks if the directory exists.
filesReturns the iterator over the files inside of the directory.
getDirectoryReturns the directory object for a name inside of the directory, the directory does not have to exist.
getFileReturns the file object for a name inside of the directory, the file does not have to exist.
moveMoves the directory with everything in it, the target directory must exist, asynchronously inside of a coroutine.
renameRenames the directory, it stays in its parent directory, asynchronously inside of a coroutine.
searchLists files and directories in the directory by wildcard, subdirectories are not searched.
touchLike Linux touch: creates the directory with missing parent directories, or updates its modification time.

#Directory:delete

sharedmenusource
function Directory:delete(recursive?: boolean, force?: boolean): boolean

Deletes the directory, the object stays usable and touch creates it again.

Parameters
NameTypeDescription
recursiveoptionalbooleanIf true, the directory is deleted with everything in it, otherwise it must be empty.
forceoptionalbooleanIf true, the directory is deleted with everything in it, a missing directory or a failed deletion are ignored instead of raising errors.
Returns
TypeNameDescription
booleandeletedtrue if the directory was deleted, can be false only when force is true.

#Directory:directories

sharedmenunodiscardsource
function Directory:directories(recursive?: boolean): fun(): Directory | nil

Returns the iterator over the directories inside of the directory.

With recursive, directories of subdirectories are returned too, each directory is listed when the loop gets to it, so changes made inside of the loop are seen.

lua
for directory in directory:directories( true ) do
    print( directory.path )
end
Parameters
NameTypeDescription
recursiveoptionalbooleanIf true, directories of subdirectories are returned too, false by default.
Returns
TypeNameDescription
fun(): Directory | niliteratorThe iterator that returns the directory objects.

#Directory:exists

sharedmenunodiscardsource
function Directory:exists(): boolean

Checks if the directory exists.

Returns
TypeNameDescription
booleanexiststrue if the directory exists, false otherwise.

#Directory:files

sharedmenunodiscardsource
function Directory:files(recursive?: boolean): fun(): File | nil

Returns the iterator over the files inside of the directory.

With recursive, files of subdirectories are returned too, each directory is listed when the loop gets to it, so changes made inside of the loop are seen.

lua
for file in directory:files( true ) do
    print( file.path )
end
Parameters
NameTypeDescription
recursiveoptionalbooleanIf true, files of subdirectories are returned too, false by default.
Returns
TypeNameDescription
fun(): File | niliteratorThe iterator that returns the file objects.

#Directory:getDirectory

sharedmenunodiscardsource
function Directory:getDirectory(name: string): Directory

Returns the directory object for a name inside of the directory, the directory does not have to exist.

Parameters
NameTypeDescription
namestringThe name of the directory.
Returns
TypeNameDescription
DirectorydirectoryThe directory object.

#Directory:getFile

sharedmenunodiscardsource
function Directory:getFile(name: string): File

Returns the file object for a name inside of the directory, the file does not have to exist.

Parameters
NameTypeDescription
namestringThe name of the file.
Returns
TypeNameDescription
FilefileThe file object.

#Directory:move

sharedmenuasyncsource
async function Directory:move(target_path: string, overwrite?: boolean)

Moves the directory with everything in it, the target directory must exist, asynchronously inside of a coroutine.

The object follows the directory, its path and name are updated, objects of files and directories inside of it keep their old paths.

Parameters
NameTypeDescription
target_pathstringThe new absolute path of the directory.
overwriteoptionalbooleanIf true, existing files are overwritten and directories are merged, otherwise the target must not exist.

#Directory:rename

sharedmenuasyncsource
async function Directory:rename(name: string, overwrite?: boolean)

Renames the directory, it stays in its parent directory, asynchronously inside of a coroutine.

The object follows the directory, its path and name are updated, objects of files and directories inside of it keep their old paths.

Parameters
NameTypeDescription
namestringThe new name of the directory.
overwriteoptionalbooleanIf true, existing files are overwritten and directories are merged, otherwise the target must not exist.

#Directory:touch

sharedmenusource
function Directory:touch()

Like Linux touch: creates the directory with missing parent directories, or updates its modification time.

Generated from the dreamwork meta branch.