Garry's Mod Wiki

file

You might be looking for the File class, which has the same name as this library.

The file library provides functions for finding, reading and writing to files.
The following path values are most commonly used:

  • LUA searches the lua files (in /lua/, in your gamemodes, in all the addons).
  • GAME searches all the mounted content (main folder, addons, mounted games etc).
  • MOD searches only the garrysmod folder.
  • DATA searches in the data folder.

See File Search Paths for the descriptive list of path values. For the full list of path values, type path in the console.

Methods

file.Append( string name, string content )
Appends a file relative to the data folder.
number file.AsyncRead( string fileName, string gamePath, function callback, boolean sync = false )
Returns the content of a file asynchronously. All limitations of file. Read also apply. This function is currently broken in the Menu State and won't be fixed. Bug Report
file.CreateDir( string name )
Creates a directory that is relative to the data folder.
file.Delete( string name, string path )
Deletes a file or empty folder that is relative to the data folder. You can't remove any files outside of data folder. You are able to delete any file in the Menu state.
boolean file.Exists( string name, string gamePath )
Returns a boolean of whether the file or directory exists or not.
table, table file.Find( string name, string path, string sorting = "nameasc" )
Returns a list of files and directories inside a single folder. It seems that paths with capital letters (e. g. lua/MyFolder/*) don't work as expected on Linux.
boolean file.IsDir( string fileName, string gamePath )
Returns whether the given file is a directory or not.
file_class file.Open( string fileName, string fileMode, string gamePath )
Attempts to open a file with the given mode.
string file.Read( string fileName, string gamePath = "GAME or DATA" )
Returns the content of a file. Beware of casing -- some filesystems are case-sensitive. SRCDS on Linux seems to force file/directory creation to lowercase, but will not modify read operations.
boolean file.Rename( string orignalFileName, string targetFileName )
Attempts to rename a file with the given name to another given name. This function is constrained to the data/ folder.
file.Size( string fileName, string gamePath )
Returns the file's size in bytes. If the file is not found, returns -1.
number file.Time( string path, string gamePath )
Returns when the file or folder was last modified in Unix time.
file.Write( string fileName, string content )
Writes the given string to a file. Erases all previous data in the file. To add data without deleting previous data, use file. Append.