fs (File System)

The fs namespace contains functions that are used for working with file system operations. Lua does contain some basic file I/O functionality, but this namespace includes a handful of additional functions that make up for where Lua is lacking.

These file system functions make use of the C++ std::filesystem header. The functions are named the same/similar.
For more information on those functions, please visit: https://en.cppreference.com/w/cpp/filesystem

Functions exposed by this namespace are accessed via the prefix: hook.fs.

Functions

create_directory, create_dir

Creates the given directory. Any folder within the given path that do not exist will also be created.

1boolean hook.fs.create_directory(path);
2boolean hook.fs.create_dir(path);
Parameter NameTypeDescription
pathstringThe name, or path, of the folder to create.
Returns
(boolean) True on success, false otherwise.

current_directory, current_dir

Returns, or sets, the current directory.

1string hook.fs.current_directory();
2string hook.fs.current_dir();
3
4boolean hook.fs.current_directory(path);
5boolean hook.fs.current_dir(path);
Parameter NameTypeDescription
pathstring[Optional] The path to set the current directory to, if setting.
Returns
get: (string) The current directory path.
set: (boolean) True on success, false otherwise.

equivalent, equals

Returns if two paths are the equal.

1boolean hook.fs.equivalent(path1, path2);
2boolean hook.fs.equals(path1, path2);
Parameter NameTypeDescription
path1stringThe first path to compare.
path2stringThe second path to compare.
Returns
(boolean) True if the paths are equal, false otherwise.

exists

Returns if the given path exists. Can be a file or folder path.

1boolean hook.fs.exists(path);
Parameter NameTypeDescription
pathstringThe path to check.
Returns
(boolean) True if the path exists, false otherwise.

get_directory, get_dir

Returns the contents of the given directory.

1table|nil hook.fs.get_directory(path);
2table|nil hook.fs.get_dir(path);
Parameter NameTypeDescription
pathstringThe path to obtain the contents of.
maskstring[Optional] The filter mask to apply when returning files.
subsboolean[Optional] Flag that states if sub folders should be included.
Returns
(table | nil) Table of the directory contents on success, nil otherwise.

normalize

Returns the normalized path.

1string|nil hook.fs.normalize(path);
Parameter NameTypeDescription
pathstringThe path to normalize.
Returns
(string | nil) The normalized path on success, nil otherwise.

remove

Removes, or deletes, the given path. Can be a file or folder path.

1boolean hook.fs.remove(path);
Parameter NameTypeDescription
pathstringThe path to remove.
Returns
(boolean) True on success, false otherwise.

rename

Renames the file or folder.

1boolean hook.fs.rename(path);
Parameter NameTypeDescription
pathstringThe path to rename.
Returns
(boolean) True on success, false otherwise.

size

Returns the size of the given path. Can be a file or folder.

1number hook.fs.size(path);
Parameter NameTypeDescription
pathstringThe path to obtain the size of.
Returns
(number) The size on success, 0 otherwise.

space

Returns the space of the given path. Can be a file or folder.

1table|nil hook.fs.space(path);
Parameter NameTypeDescription
pathstringThe path to obtain the space of.
Returns
(table | nil) Table containing the space information on success, nil otherwise.

The returned table contains the following properties:

  • available (number) - The total size of the file system. (in bytes)
  • capacity (number) - The free space of the file system. (in bytes)
  • free (number) - The free space of the file system, to a non-privileged process. (in bytes)

status

Returns the status of the given path. Can be a file or folder.

1table|nil hook.fs.status(path);
Parameter NameTypeDescription
pathstringThe path to obtain the status of.
Returns
(table | nil) Table containing the status information on success, nil otherwise.

The returned table contains the following properties:

  • permissions (number) - The file or folder permissions.
  • exists (boolean) - Flag that states if the path exists.
  • is_regular_file (boolean) - Flag that states if the path is a regular file.
  • is_directory (boolean) - Flag that states if the path is a directory.
  • is_block_file (boolean) - Flag that states if the path is a block file.
  • is_character_file (boolean) - Flag that states if the path is a character file.
  • is_fifo (boolean) - Flag that states if the path is a FIFO file.
  • is_socket (boolean) - Flag that states if the path is a socket.
  • is_symlink (boolean) - Flag that states if the path is a symbolic link.

Properties

preferred_separator

Contains the preferred slash character for paths.

1local sep = hook.fs.preferred_separator;
2print(sep); -- Prints \