dreamworkAPI
Ctrl K
GitHub
classDocs / File

File

classsharedmenusource
Object → File

The file object.

A thin handle over a vfs path, the file is not opened or cached, every call goes to vfs, so the object can exist before its file.

Fields path, name and content are backed by vfs, assigning them moves, renames or writes the file, reading and writing are asynchronous inside of a coroutine and synchronous outside of it.

lua
local file = File( "/garrysmod/data/test.txt" )
file.content = "hello" -- write
print( file.content, file.size ) -- "hello" 5
file.name = "renamed.txt" -- rename
file.path = "/garrysmod/data/other/test.txt" -- move

Constructor

File(file_path: string, touch: boolean?): File

Fields

NameTypeDescription
pathstringThe absolute virtual path of the file, assigning moves the file without overwriting, the target directory must exist.
namestringThe name of the file with its extension, assigning renames the file without overwriting.
contentstringThe file data, reading reads the whole file, assigning replaces it.
sizeintegerThe size of the file in bytes, 0 if the file does not exist. Read-only.
parentDirectoryThe directory the file is in, assigning moves the file into the directory without overwriting.

Static functions

#File.exists

sharedmenunodiscardsource
function File.exists(file_path: string): boolean

Checks if a file exists.

Parameters
NameTypeDescription
file_pathstringThe absolute path to the file.
Returns
TypeNameDescription
booleanexiststrue if the file exists, false otherwise.

Methods

appendAppends data to the end of the file, the file and missing directories are created, asynchronously inside of a coroutine.
deleteDeletes the file, the object stays usable and write or touch creates it again.
existsChecks if the file exists.
moveMoves the file, the target directory must exist, asynchronously inside of a coroutine.
readReads the whole file, asynchronously inside of a coroutine.
renameRenames the file, it stays in its directory, asynchronously inside of a coroutine.
touchLike Linux touch: creates an empty file with missing directories, or updates the modification time keeping its content.
writeReplaces the file data, the file and missing directories are created, asynchronously inside of a coroutine.

#File:append

sharedmenuasyncsource
async function File:append(data: string)

Appends data to the end of the file, the file and missing directories are created, asynchronously inside of a coroutine.

Parameters
NameTypeDescription
datastringThe data to append.

#File:delete

sharedmenusource
function File:delete(force?: boolean): boolean

Deletes the file, the object stays usable and write or touch creates it again.

Parameters
NameTypeDescription
forceoptionalbooleanIf true, a missing file or a failed deletion are ignored instead of raising errors.
Returns
TypeNameDescription
booleandeletedtrue if the file was deleted, can be false only when force is true.

#File:exists

sharedmenunodiscardsource
function File:exists(): boolean

Checks if the file exists.

Returns
TypeNameDescription
booleanexiststrue if the file exists, false otherwise.

#File:move

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

Moves the file, the target directory must exist, asynchronously inside of a coroutine.

The object follows the file, its path and name are updated.

Parameters
NameTypeDescription
target_pathstringThe new absolute path of the file.
overwriteoptionalbooleanIf true, the existing file is replaced, otherwise the target must not exist.

#File:read

sharedmenuasyncnodiscardsource
async function File:read(): string

Reads the whole file, asynchronously inside of a coroutine.

Returns
TypeNameDescription
stringdataThe file data.

#File:rename

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

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

The object follows the file, its path and name are updated.

Parameters
NameTypeDescription
namestringThe new name of the file.
overwriteoptionalbooleanIf true, the existing file is replaced, otherwise the target must not exist.

#File:touch

sharedmenusource
function File:touch()

Like Linux touch: creates an empty file with missing directories, or updates the modification time keeping its content.

#File:write

sharedmenuasyncsource
async function File:write(data: string)

Replaces the file data, the file and missing directories are created, asynchronously inside of a coroutine.

Parameters
NameTypeDescription
datastringThe new data.
Generated from the dreamwork meta branch.