path
The file path library.
Works with POSIX-style paths: / is the only separator and a path is absolute when it starts with /. Backslashes are ordinary bytes, except in stripTrailingSlash, stripLeadingSlash and normalizeSlashes, which also treat \ as a separator.
Nothing here touches the file system; all functions work on the strings only.
Classes & types Fields Name Type Description delimiterstring The delimiter between paths in a path list, always ":". = ":" sepstring The path segment separator, always "/". = "/"
Functions path.ensureLeadingSlashAdds a leading / to the file path if it does not start with one, which makes a relative path absolute. path.ensureTrailingSlashAdds a trailing / to the file path if it does not end with one. path.equalspath.getReturns the path of the file that a function was loaded from. path.getDirectoryReturns the directory, the part before the last /. path.getExtensionReturns the extension of the file name, the part after its last dot. path.getFileReturns the file name, the part after the last /, without its extension by default. path.isAbsoluteChecks if the file path is absolute, i.e. starts with /. path.isRelativeChecks if the file path is relative, i.e. does not start with /. path.joinJoins the file paths with / and normalizes the result (see path.normalize). path.normalizeNormalizes a file path: collapses repeated slashes, removes . segments and resolves .. segments against the segments be… path.normalizeSlashesReplaces every run of slashes (/ and \) with a single /. path.parseParses a file path into a table of its parts. path.relativeReturns the relative path from the from directory to to. path.replaceDirectoryReplaces the directory, the part before the last /, and keeps the file name. path.replaceExtensionReplaces the extension of the file name, or adds one if there is none. path.replaceNameReplaces the file name (with extension), the part after the last /. path.resolveResolves a file path to an absolute, normalized file path. path.splitSplits a file path at the last / into a directory and a file name. path.splitExtensionSplits a file path at the last dot of the file name into the path without the extension and the extension. path.stripLeadingSlashRemoves all leading slashes (/ and \) from the file path, which makes an absolute path relative. path.stripTrailingSlashRemoves all trailing slashes (/ and \) from the file path. path.wildcardConverts a wildcard (glob) string to a Lua pattern.
function path.ensureLeadingSlash (file_path : string ): string
Adds a leading / to the file path if it does not start with one, which makes a relative path absolute.
lua Copy
path.ensureLeadingSlash ( "game/lua" ) -- "/game/lua"
path.ensureLeadingSlash ( "/game/lua" ) -- "/game/lua"
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description string new_file_pathThe new file path.
# path.ensureTrailingSlash
function path.ensureTrailingSlash (file_path : string ): string
Adds a trailing / to the file path if it does not end with one.
lua Copy
path.ensureTrailingSlash ( "/game/lua" ) -- "/game/lua/"
path.ensureTrailingSlash ( "/game/lua/" ) -- "/game/lua/"
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description string new_file_pathThe new file path.
function path.equals (file_path1 : string , file_path2 : string ): boolean
Parameters
Name Type Description file_path1string file_path2string
Returns
Type Name Description boolean is_equal
function path.get (f? : ( function | integer )?): string
Returns the path of the file that a function was loaded from.
With a stack level, the __file object in the function's environment is used first (set for files loaded by dreamwork), then the source of the main chunk on the stack. With a function, its source is used.
If the file can't be found, "/game/lua/unknown.lua" is returned.
lua Copy
-- in /game/lua/autorun/init.lua
path.get () -- "/game/lua/autorun/init.lua"
Parameters
Name Type Description foptional ( function | integer )?The function, or the stack level, where 1 (default) is the function calling get.
Returns
Type Name Description string file_pathThe absolute path of the file.
function path.getDirectory (file_path : string , keep_trailing_slash? : boolean ): string | nil
Returns the directory, the part before the last /.
The root directory is always returned as "/", even if keep_trailing_slash is not true.
lua Copy
path.getDirectory ( "/game/lua/init.lua" ) -- "/game/lua"
path.getDirectory ( "/game/lua/init.lua" , true ) -- "/game/lua/"
path.getDirectory ( "/init.lua" ) -- "/"
path.getDirectory ( "init.lua" ) -- nil
Parameters
Name Type Description file_pathstring The file path. keep_trailing_slashoptional boolean true to keep the trailing slash, false otherwise.
Returns
Type Name Description string | nil directoryThe directory of the file, or nil if the file path has no /.
function path.getExtension (file_path : string , keep_dot? : boolean ): string
Returns the extension of the file name, the part after its last dot.
A leading dot is part of the name, so dotfiles like .bashrc have no extension.
lua Copy
path.getExtension ( "/game/lua/init.lua" ) -- "lua"
path.getExtension ( "/game/lua/init.lua" , true ) -- ".lua"
path.getExtension ( "archive.tar.gz" ) -- "gz"
path.getExtension ( "/home/.bashrc" ) -- ""
path.getExtension ( "/data.d/file" ) -- ""
Parameters
Name Type Description file_pathstring The file path. keep_dotoptional boolean true to keep the dot, false otherwise.
Returns
Type Name Description string extensionThe extension of the file, or an empty string if there is none.
function path.getFile (file_path : string , keep_extension? : boolean ): string
Returns the file name, the part after the last /, without its extension by default.
Only the last extension is removed, and a leading dot is part of the name, so dotfiles like .bashrc have no extension.
lua Copy
path.getFile ( "/game/lua/init.lua" ) -- "init"
path.getFile ( "/game/lua/init.lua" , true ) -- "init.lua"
path.getFile ( "archive.tar.gz" ) -- "archive.tar"
path.getFile ( "/home/.bashrc" ) -- ".bashrc"
path.getFile ( "/game/lua/" ) -- ""
Parameters
Name Type Description file_pathstring The file path. keep_extensionoptional boolean true to keep the extension, false otherwise.
Returns
Type Name Description string file_nameThe name of the file.
function path.isAbsolute (file_path : string ): boolean
Checks if the file path is absolute, i.e. starts with /.
lua Copy
path.isAbsolute ( "/game/lua/init.lua" ) -- true
path.isAbsolute ( "lua/init.lua" ) -- false
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description boolean is_abstrue if the file path is absolute, false otherwise.
function path.isRelative (file_path : string ): boolean
Checks if the file path is relative, i.e. does not start with /.
An empty string is relative.
lua Copy
path.isRelative ( "lua/init.lua" ) -- true
path.isRelative ( "/game/lua/init.lua" ) -- false
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description boolean is_reltrue if the file path is relative, false otherwise.
function path.join (segments : string[] , segment_count? : integer ): string
Joins the file paths with / and normalizes the result (see path.normalize ).
Empty paths are skipped, and a leading / is kept only on the first path, so the result is absolute only if the first non-empty path is. The segments table is not modified.
lua Copy
path.join ( { "/game" , "lua/" , "autorun/init.lua" } ) -- "/game/lua/autorun/init.lua"
path.join ( { "/game/lua" , "../data" , "x.txt" } ) -- "/game/data/x.txt"
path.join ( { "lua" , "/autorun" } ) -- "lua/autorun"
path.join ( {} ) -- "."
Parameters
Name Type Description segmentsstring[] The file paths to join. segment_countoptional integer The number of file paths to join, defaults to the length of segments.
Returns
Type Name Description string file_pathThe joined file path.
function path.normalize (file_path : string , keep_trailing_slash? : boolean ): string
Normalizes a file path: collapses repeated slashes, removes . segments and resolves .. segments against the segments before them.
.. segments that can't be resolved stay at the start of a relative path, and are dropped from an absolute path, since nothing is above the root. An empty result becomes "." (or "/" for absolute paths).
Already normalized paths are returned as is, without creating a new string.
lua Copy
path.normalize ( "/game//lua/./x/../init.lua" ) -- "/game/lua/init.lua"
path.normalize ( "lua/../../init.lua" ) -- "../init.lua"
path.normalize ( "/../init.lua" ) -- "/init.lua"
path.normalize ( "lua/.." ) -- "."
path.normalize ( "lua/x/" ) -- "lua/x"
path.normalize ( "lua/x/" , true ) -- "lua/x/"
Parameters
Name Type Description file_pathstring The file path. keep_trailing_slashoptional boolean true to keep the trailing slash, false otherwise.
Returns
Type Name Description string new_file_pathThe normalized file path.
function path.normalizeSlashes (file_path : string ): string
Replaces every run of slashes (/ and \) with a single /.
Useful for paths that came from Windows. Unlike path.normalize , . and .. segments are left as is.
lua Copy
path.normalizeSlashes ( "garrysmod\\lua//init.lua" ) -- "garrysmod/lua/init.lua"
path.normalizeSlashes ( "a\\..\\b" ) -- "a/../b"
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description string new_file_pathThe new file path.
function path.parse (file_path : string ): path.ParsedData
Parses a file path into a table of its parts.
┌─────────────────────┬────────────┐ │ dir │ base │ ├──────┬ ├──────┬─────┤ │ root │ │ name │ ext │ " / home/user/dir/ file .txt " └──────┴──────────────┴──────┴─────┘ (All spaces in the "" line should be ignored. They are purely for formatting.)
dir keeps its trailing slash and ext keeps its dot, so dir .. name .. ext is always the original path. The path is not normalized.
lua Copy
local data = path.parse ( "/home/user/dir/file.txt" )
-- data.root = "/", data.dir = "/home/user/dir/", data.base = "file.txt"
-- data.name = "file", data.ext = ".txt", data.abs = true
Parameters
Name Type Description file_pathstring The file path.
Returns
function path.relative (from : string , to : string , stack_level? : integer ): string
Returns the relative path from the from directory to to.
Both paths are resolved with path.resolve first, so relative paths are relative to the calling file. Segments are compared with path.equals .
If both resolve to the same path, "." is returned.
lua Copy
path.relative ( "/game/lua/autorun" , "/game/lua/shared/util.lua" ) -- "../shared/util.lua"
path.relative ( "/game/lua" , "/game/lua/autorun/init.lua" ) -- "autorun/init.lua"
path.relative ( "/game/lua/autorun" , "/game" ) -- "../.."
path.relative ( "/game/lua" , "/game/lua/" ) -- "."
Parameters
Name Type Description fromstring The directory to get the relative path from. tostring The path to get the relative path to. stack_leveloptional integer The stack level of the file to resolve relative paths against, where 1 (default) is the function calling relative.
Returns
Type Name Description string relative_pathThe relative path.
function path.replaceDirectory (file_path : string , dir_name : string ): string
Replaces the directory, the part before the last /, and keeps the file name.
A / is added between the directory and the file name if needed. An empty directory leaves only the file name.
lua Copy
path.replaceDirectory ( "/game/lua/init.lua" , "/data/cache" ) -- "/data/cache/init.lua"
path.replaceDirectory ( "/game/lua/init.lua" , "/data/cache/" ) -- "/data/cache/init.lua"
path.replaceDirectory ( "/game/lua/init.lua" , "" ) -- "init.lua"
Parameters
Name Type Description file_pathstring The file path. dir_namestring The new directory, with or without a trailing slash.
Returns
Type Name Description string new_file_pathThe new file path.
function path.replaceExtension (file_path : string , ext_name : string ): string
Replaces the extension of the file name, or adds one if there is none.
lua Copy
path.replaceExtension ( "/game/lua/init.lua" , "txt" ) -- "/game/lua/init.txt"
path.replaceExtension ( "archive.tar.gz" , "zip" ) -- "archive.tar.zip"
path.replaceExtension ( "/home/.bashrc" , "bak" ) -- "/home/.bashrc.bak"
Parameters
Name Type Description file_pathstring The file path. ext_namestring The new extension, without the leading dot.
Returns
Type Name Description string new_file_pathThe new file path.
function path.replaceName (file_path : string , file_name : string ): string
Replaces the file name (with extension), the part after the last /.
lua Copy
path.replaceName ( "/game/lua/init.lua" , "shared.lua" ) -- "/game/lua/shared.lua"
path.replaceName ( "init.lua" , "shared.lua" ) -- "shared.lua"
Parameters
Name Type Description file_pathstring The file path. file_namestring The new file name, including its extension.
Returns
Type Name Description string new_file_pathThe new file path.
function path.resolve (file_path : string , stack_level? : integer ): string
Resolves a file path to an absolute, normalized file path.
Absolute paths are only normalized. Relative paths are resolved against the directory of the file that calls resolve (see path.get ), similar to require.
lua Copy
-- in /game/lua/autorun/init.lua
path.resolve ( "client.lua" ) -- "/game/lua/autorun/client.lua"
path.resolve ( "../shared/util.lua" ) -- "/game/lua/shared/util.lua"
path.resolve ( "/data/./x.txt" ) -- "/data/x.txt"
Parameters
Name Type Description file_pathstring The file path. stack_leveloptional integer The stack level of the file to resolve against, where 1 (default) is the function calling resolve.
Returns
Type Name Description string abs_pathThe resolved file path.
function path.split (file_path : string , keep_trailing_slash? : boolean ): string , string
Splits a file path at the last / into a directory and a file name.
The root directory is always returned as "/", even if keep_trailing_slash is not true.
lua Copy
path.split ( "/game/lua/init.lua" ) -- "/game/lua", "init.lua"
path.split ( "/game/lua/init.lua" , true ) -- "/game/lua/", "init.lua"
path.split ( "/init.lua" ) -- "/", "init.lua"
path.split ( "init.lua" ) -- "", "init.lua"
Parameters
Name Type Description file_pathstring The file path. keep_trailing_slashoptional boolean true to keep the trailing slash, false otherwise.
Returns
Type Name Description string directory_pathThe directory from the file path, or an empty string if the file path has no /. string file_nameThe file name with extension from the file path.
function path.splitExtension (file_path : string , keep_dot? : boolean ): string , string
Splits a file path at the last dot of the file name into the path without the extension and the extension.
A leading dot is part of the name, so dotfiles like .bashrc have no extension.
lua Copy
path.splitExtension ( "/game/lua/init.lua" ) -- "/game/lua/init", "lua"
path.splitExtension ( "/game/lua/init.lua" , true ) -- "/game/lua/init", ".lua"
path.splitExtension ( "/home/.bashrc" ) -- "/home/.bashrc", ""
Parameters
Name Type Description file_pathstring The file path. keep_dotoptional boolean true to keep the extension dot, false otherwise.
Returns
Type Name Description string file_path_without_extensionThe file path without the extension. string extensionThe extension, or an empty string if there is none.
function path.stripLeadingSlash (file_path : string ): string
Removes all leading slashes (/ and \) from the file path, which makes an absolute path relative.
lua Copy
path.stripLeadingSlash ( "/game/lua" ) -- "game/lua"
path.stripLeadingSlash ( "\\/game/lua" ) -- "game/lua"
path.stripLeadingSlash ( "/" ) -- ""
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description string new_file_pathThe new file path.
function path.stripTrailingSlash (file_path : string ): string
Removes all trailing slashes (/ and \) from the file path.
The first byte is never removed, so the root directory stays "/".
lua Copy
path.stripTrailingSlash ( "/game/lua/" ) -- "/game/lua"
path.stripTrailingSlash ( "lua\\//" ) -- "lua"
path.stripTrailingSlash ( "/" ) -- "/"
Parameters
Name Type Description file_pathstring The file path.
Returns
Type Name Description string new_file_pathThe new file path.
function path.wildcard (wildcard_str : string , anchor_to_slash? : boolean ): string
Converts a wildcard (glob) string to a Lua pattern.
Supported syntax:
? - any single byte except /.* - any sequence of bytes except /.** - any sequence of bytes, **/ also matches zero directories.[abc], [a-z] - one byte from the set.[!a-z], [^a-z] - one byte not in the set, never /.\x - the literal byte x, e.g. \* or \[.A ] right after [ or [! is a member of the set, e.g. []]. A [ without a closing ] is a literal [. Other bytes match themselves, Lua pattern magic characters are escaped. Brace expansion ({a,b}) is not supported.
The pattern always has to match up to the end of the string. By default it also has to match from the start; with anchor_to_slash it can start at the beginning or right after any /, so it matches the last segments of a full path.
lua Copy
path.wildcard ( "*.lua" ) -- "^[^/]*%.lua$"
path.wildcard ( "**/init.lua" ) -- "^.*%f[^%z/]init%.lua$"
string.match ( "init.lua" , path.wildcard ( "*.lua" ) ) -- "init.lua"
string.match ( "lua/init.lua" , path.wildcard ( "*.lua" ) ) -- nil
string.match ( "/game/lua/init.lua" , path.wildcard ( "*.lua" , true ) ) -- "init.lua"
string.match ( "x2.txt" , path.wildcard ( "x[0-9].txt" ) ) -- "x2.txt"
Parameters
Name Type Description wildcard_strstring The path wildcard string. anchor_to_slashoptional boolean If true, the pattern can match after any / instead of only at the start of the string.
Returns
Type Name Description string pattern_strThe Lua pattern that represents the wildcard string.