Extensions of the File library, which reads and writes files anywhere in the game folder. The library itself comes from the file binary module that the server opens on startup. This file replaces File.write with a version that creates missing folders, and adds recursive listing, wrappers around the built-in file functions that always work in the game folder, and aliases such as File.rm, File.dir and File.is_folder.

Summary

Functions

Checks whether a file or folder exists.
Returns the extension of a file name.
Finds the files and folders that match a wildcard.
Lists all files inside of a folder and its subfolders.
Checks whether a path points to a folder.
Lists the names of the files and folders that match a wildcard as a single array.
Returns the file name part of a path.
Returns the folder part of a path.
Returns the size of a file.
Returns the time a file or folder was last modified at.
Creates a file if it does not exist yet, by appending an empty string to it.
Writes a string to a file, creating the missing folders of its path first.

Functions

File.exists(filename)

shared lib/stdlib/sh_file.lua:72

Checks whether a file or folder exists.

Parameters

  • filename String path relative to the game folder

Returns

  • Boolean

File.ext(filename)

shared lib/stdlib/sh_file.lua:108

Returns the extension of a file name.

Parameters

  • filename String file name or path

Returns

  • String extension without the dot, or nil if there is none

File.find(filename, sort)

shared lib/stdlib/sh_file.lua:80

Finds the files and folders that match a wildcard.

Parameters

  • filename String path with a wildcard relative to the game folder, e.g. 'gamemodes/*'
  • sort String optional, defaults to 'nameasc' sorting order: 'nameasc', 'namedesc', 'dateasc' or 'datedesc'

Returns

  • List<String> file names
  • List<String> folder names

File.get_list(folder)

shared lib/stdlib/sh_file.lua:33

Lists all files inside of a folder and its subfolders. Subfolders whose name starts with a dot are skipped.

Parameters

  • folder String folder path relative to the game folder

Returns

  • List<String> file paths, each one starting with the folder path

File.is_dir(filename)

shared lib/stdlib/sh_file.lua:87

Checks whether a path points to a folder.

Parameters

  • filename String path relative to the game folder

Returns

  • Boolean

File.ls(path, include_hidden)

shared lib/stdlib/sh_file.lua:130

Lists the names of the files and folders that match a wildcard as a single array.

Parameters

  • path String path with a wildcard relative to the game folder, e.g. 'gamemodes/*'
  • include_hidden Boolean optional, defaults to false include entries whose name starts with a dot

Returns

  • List<String> file names followed by folder names, or nil if the search failed

File.name(filename)

shared lib/stdlib/sh_file.lua:115

Returns the file name part of a path.

Parameters

  • filename String file path

Returns

  • String file name including its extension

File.path(filename)

shared lib/stdlib/sh_file.lua:122

Returns the folder part of a path.

Parameters

  • filename String file path

Returns

  • String path up to and including the last slash

File.size(filename)

shared lib/stdlib/sh_file.lua:94

Returns the size of a file.

Parameters

  • filename String file path relative to the game folder

Returns

  • Number size in bytes, -1 if the file does not exist

File.time(filename)

shared lib/stdlib/sh_file.lua:101

Returns the time a file or folder was last modified at.

Parameters

  • filename String path relative to the game folder

Returns

  • Number UNIX timestamp, 0 if the file does not exist

File.touch(filename)

shared lib/stdlib/sh_file.lua:65

Creates a file if it does not exist yet, by appending an empty string to it.

Parameters

  • filename String file path relative to the game folder

Returns

  • Any return value of File.append

File.write(file_name, file_contents)

shared lib/stdlib/sh_file.lua:15

Writes a string to a file, creating the missing folders of its path first. The path is relative to the game folder (garrysmod/).

Parameters

  • file_name String file path, e.g. 'settings/flux/config.json'
  • file_contents String contents to write

Defined in