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.
file.CreateDir( string name )
Creates a directory that is relative to the data folder.
file.Delete( string name )
Deletes a file or empty folder that is relative to the data folder. You can't remove any files outside of data folder.
boolean file.Exists( string name, string gamePath )
Returns a boolean of whether the file or directory exists or not. This will sometimes return false clientside for directories received from the server via a clientside lua file. You can work around this by using file.Find with the path to the directory followed by a wildcard (no trailing forward slash) and see if the directory is inside the returned directories table (see second example).Issue Tracker: 1038
table, table file.Find( string name, string path, string sorting = "nameasc" )
Returns a list of files and directories inside a single folder.
boolean file.IsDir( string fileName, string gamePath )
Returns whether the given file is a directory or not. This will sometimes return false clientside for directories received from the server via a clientside lua file. You can work around this by using file.Find with the path to the directory followed by a wildcard (no trailing forward slash) and see if the directory is inside the returned directories table (see second example).Issue Tracker: 1038
File file.Open( string fileName, string fileMode, string gamePath )
Attempts to open a file with the given mode.
string file.Read( string fileName, string gamePath )
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 lasted 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.

Page Links


Special Pages


Wikis

?

Render Time: 34ms

DB GetPage 7
Generate Html 3
SaveChanges (1) 12
Render Body 0
Render Sidebar 9