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 Copy
local directory = Directory ( "/garrysmod/data/test" , true )
local files, file_count = directory:search ( "*.txt" )
Constructor Directory (directory_path : string , touch : boolean ?): Directory Fields Name Type Description pathstring The absolute virtual path of the directory, assigning moves the directory without overwriting, the target directory must exist. namestring The name of the directory, empty for the root, assigning renames the directory without overwriting. sizeinteger The total size of everything in the directory in bytes, walked recursively, 0 if the directory does not exist. Read-only. parentDirectory The 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.
function Directory.exists (directory_path : string ): boolean
Checks if a directory exists.
Parameters
Name Type Description directory_pathstring The absolute path to the directory.
Returns
Type Name Description boolean existstrue if the directory exists, false otherwise.
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 Copy
local files, file_count, directories, directory_count = Directory.search ( "/garrysmod/data" , "*.txt" )
Parameters
Name Type Description directory_pathstring The absolute path to the directory. wildcardoptional string The wildcard names must match, see path.wildcard , * by default.
Returns
Type Name Description File[] filesThe found files. integer file_countThe number of the found files. Directory[] directoriesThe found directories. integer directory_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.
function Directory:delete (recursive? : boolean , force? : boolean ): boolean
Deletes the directory, the object stays usable and touch creates it again.
Parameters
Name Type Description recursiveoptional boolean If true, the directory is deleted with everything in it, otherwise it must be empty. forceoptional boolean If true, the directory is deleted with everything in it, a missing directory or a failed deletion are ignored instead of raising errors.
Returns
Type Name Description boolean deletedtrue if the directory was deleted, can be false only when force is true.
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 Copy
for directory in directory:directories ( true ) do
print ( directory.path )
end
Parameters
Name Type Description recursiveoptional boolean If true, directories of subdirectories are returned too, false by default.
Returns
Type Name Description fun (): Directory | nil iteratorThe iterator that returns the directory objects.
function Directory:exists (): boolean
Checks if the directory exists.
Returns
Type Name Description boolean existstrue if the directory exists, false otherwise.
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 Copy
for file in directory:files ( true ) do
print ( file.path )
end
Parameters
Name Type Description recursiveoptional boolean If true, files of subdirectories are returned too, false by default.
Returns
Type Name Description fun (): File | nil iteratorThe iterator that returns the file objects.
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
Name Type Description namestring The name of the directory.
Returns
Type Name Description Directory directoryThe directory object.
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
Name Type Description namestring The name of the file.
Returns
Type Name Description File fileThe file object.
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
Name Type Description target_pathstring The new absolute path of the directory. overwriteoptional boolean If true, existing files are overwritten and directories are merged, otherwise the target must not exist.
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
Name Type Description namestring The new name of the directory. overwriteoptional boolean If true, existing files are overwritten and directories are merged, otherwise the target must not exist.
function Directory:search (wildcard? : string ): File[] , integer , Directory[] , integer
Lists files and directories in the directory by wildcard, subdirectories are not searched.
lua Copy
local files, file_count, directories, directory_count = directory:search ( "*.txt" )
Parameters
Name Type Description wildcardoptional string The wildcard names must match, see path.wildcard , * by default.
Returns
Type Name Description File[] filesThe found files. integer file_countThe number of the found files. Directory[] directoriesThe found directories. integer directory_countThe number of the found directories.
function Directory:touch ()
Like Linux touch: creates the directory with missing parent directories, or updates its modification time.