dreamworkAPI
Ctrl K
GitHub
libraryDocs / path

path

librarysharedmenusource

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

NameTypeDescription
delimiterstringThe delimiter between paths in a path list, always ":". = ":"
sepstringThe 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.equals
path.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.

#path.ensureLeadingSlash

sharedmenunodiscardsource
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
path.ensureLeadingSlash( "game/lua" ) -- "/game/lua"
path.ensureLeadingSlash( "/game/lua" ) -- "/game/lua"
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.ensureTrailingSlash

sharedmenunodiscardsource
function path.ensureTrailingSlash(file_path: string): string

Adds a trailing / to the file path if it does not end with one.

lua
path.ensureTrailingSlash( "/game/lua" ) -- "/game/lua/"
path.ensureTrailingSlash( "/game/lua/" ) -- "/game/lua/"
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.equals

nodiscardsource
function path.equals(file_path1: string, file_path2: string): boolean
Parameters
NameTypeDescription
file_path1string
file_path2string
Returns
TypeNameDescription
booleanis_equal

#path.get

sharedmenunodiscardsource
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
-- in /game/lua/autorun/init.lua
path.get() -- "/game/lua/autorun/init.lua"
Parameters
NameTypeDescription
foptional( function | integer )?The function, or the stack level, where 1 (default) is the function calling get.
Returns
TypeNameDescription
stringfile_pathThe absolute path of the file.

#path.getDirectory

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
keep_trailing_slashoptionalbooleantrue to keep the trailing slash, false otherwise.
Returns
TypeNameDescription
string | nildirectoryThe directory of the file, or nil if the file path has no /.

#path.getExtension

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
keep_dotoptionalbooleantrue to keep the dot, false otherwise.
Returns
TypeNameDescription
stringextensionThe extension of the file, or an empty string if there is none.

#path.getFile

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
keep_extensionoptionalbooleantrue to keep the extension, false otherwise.
Returns
TypeNameDescription
stringfile_nameThe name of the file.

#path.isAbsolute

sharedmenunodiscardsource
function path.isAbsolute(file_path: string): boolean

Checks if the file path is absolute, i.e. starts with /.

lua
path.isAbsolute( "/game/lua/init.lua" ) -- true
path.isAbsolute( "lua/init.lua" ) -- false
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
booleanis_abstrue if the file path is absolute, false otherwise.

#path.isRelative

sharedmenunodiscardsource
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
path.isRelative( "lua/init.lua" ) -- true
path.isRelative( "/game/lua/init.lua" ) -- false
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
booleanis_reltrue if the file path is relative, false otherwise.

#path.join

sharedmenunodiscardsource
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
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
NameTypeDescription
segmentsstring[]The file paths to join.
segment_countoptionalintegerThe number of file paths to join, defaults to the length of segments.
Returns
TypeNameDescription
stringfile_pathThe joined file path.

#path.normalize

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
keep_trailing_slashoptionalbooleantrue to keep the trailing slash, false otherwise.
Returns
TypeNameDescription
stringnew_file_pathThe normalized file path.

#path.normalizeSlashes

sharedmenunodiscardsource
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
path.normalizeSlashes( "garrysmod\\lua//init.lua" ) -- "garrysmod/lua/init.lua"
path.normalizeSlashes( "a\\..\\b" ) -- "a/../b"
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.parse

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
path.ParsedDatadataThe parsed file path data.

#path.relative

sharedmenunodiscardsource
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
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
NameTypeDescription
fromstringThe directory to get the relative path from.
tostringThe path to get the relative path to.
stack_leveloptionalintegerThe stack level of the file to resolve relative paths against, where 1 (default) is the function calling relative.
Returns
TypeNameDescription
stringrelative_pathThe relative path.

#path.replaceDirectory

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
dir_namestringThe new directory, with or without a trailing slash.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.replaceExtension

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
ext_namestringThe new extension, without the leading dot.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.replaceName

sharedmenunodiscardsource
function path.replaceName(file_path: string, file_name: string): string

Replaces the file name (with extension), the part after the last /.

lua
path.replaceName( "/game/lua/init.lua", "shared.lua" ) -- "/game/lua/shared.lua"
path.replaceName( "init.lua", "shared.lua" ) -- "shared.lua"
Parameters
NameTypeDescription
file_pathstringThe file path.
file_namestringThe new file name, including its extension.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.resolve

sharedmenunodiscardsource
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
-- 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
NameTypeDescription
file_pathstringThe file path.
stack_leveloptionalintegerThe stack level of the file to resolve against, where 1 (default) is the function calling resolve.
Returns
TypeNameDescription
stringabs_pathThe resolved file path.

#path.split

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
keep_trailing_slashoptionalbooleantrue to keep the trailing slash, false otherwise.
Returns
TypeNameDescription
stringdirectory_pathThe directory from the file path, or an empty string if the file path has no /.
stringfile_nameThe file name with extension from the file path.

#path.splitExtension

sharedmenunodiscardsource
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
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
NameTypeDescription
file_pathstringThe file path.
keep_dotoptionalbooleantrue to keep the extension dot, false otherwise.
Returns
TypeNameDescription
stringfile_path_without_extensionThe file path without the extension.
stringextensionThe extension, or an empty string if there is none.

#path.stripLeadingSlash

sharedmenunodiscardsource
function path.stripLeadingSlash(file_path: string): string

Removes all leading slashes (/ and \) from the file path, which makes an absolute path relative.

lua
path.stripLeadingSlash( "/game/lua" ) -- "game/lua"
path.stripLeadingSlash( "\\/game/lua" ) -- "game/lua"
path.stripLeadingSlash( "/" ) -- ""
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.stripTrailingSlash

sharedmenunodiscardsource
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
path.stripTrailingSlash( "/game/lua/" ) -- "/game/lua"
path.stripTrailingSlash( "lua\\//" ) -- "lua"
path.stripTrailingSlash( "/" ) -- "/"
Parameters
NameTypeDescription
file_pathstringThe file path.
Returns
TypeNameDescription
stringnew_file_pathThe new file path.

#path.wildcard

sharedmenunodiscardsource
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
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
NameTypeDescription
wildcard_strstringThe path wildcard string.
anchor_to_slashoptionalbooleanIf true, the pattern can match after any / instead of only at the start of the string.
Returns
TypeNameDescription
stringpattern_strThe Lua pattern that represents the wildcard string.
Generated from the dreamwork meta branch.