Filesystem
This section contains documentation of functions and properties that allow you to check the existance of / delete a file, create / enumerate directories, and in other ways interact with and/or manipulate the filesystem.
If you wish to specifically read and write files, you should look at the file datastream class.
Functions
directory_create
Creates a directory if it doesn't already exist.
bool directory_create(string directory);
Arguments:
- string directory: path to the directory to create (can be nested, relative or absolute).
Returns:
true if the directory was successfully created or already exists, false otherwise.
Example:
void main() {
if (directory_exists("test")) {
alert("Info", "The test directory already exists; nothing to do");
exit();
}
if (directory_create("test")) {
alert("Info", "Directory created. Deleting...");
alert("Info", directory_delete("test") ? "Success": "Fail");
}
else
alert("Error", "Couldn't create the directory.");
}
directory_delete
Deletes a directory.
bool directory_delete(string directory);
Arguments:
- string directory: the directory to delete.
Returns:
bool: true if the directory was successfully deleted, false otherwise.
directory_exists
Determine if a directory exists or not.
bool directory_exists(string directory);
Arguments:
- string directory: the directory whose existence will be checked (can be a relative or absolute path).
Returns:
true if the directory exists, false otherwise.
Example:
void main() {
alert("Directory exists", directory_exists("test") ? "There is a test folder in the current directory" : "There is not a test folder in the current directory");
}
file_copy
Coppies a file from one location to another.
bool file_copy(string source_filename, string destination_filename);
Arguments:
- string source_filename: the path/name of the file to be coppied.
- string destination_filename: the path (including filename) to copy the file to.
Returns:
bool: true if the file was successfully coppied, false otherwise.
file_delete
Deletes a file.
bool file_delete(string filename);
Arguments:
- string filename: the name of the file to delete.
Returns:
bool: true if the file was successfully deleted, false otherwise.
file_exists
Check for the existence of a particular file.
bool file_exists(string file_path);
Arguments:
- string file_path: the path to the file to query.
Returns:
bool: true if the file exists, false otherwise.
file_get_date_created
Get a datetime object representing the creation date of a particular file.
datetime file_get_date_created(string filename);
Arguments:
- string filename; the name of the file to check.
Returns:
datetime: an initialized datetime object, with all its fields set to the creation date of the file.
Remarks:
The date returned is in UTC, not your local timezone.
Example:
void main() {
datetime dt = file_get_date_created("file_get_date_created.nvgt");
alert("file_get_date_created.nvgt was created on", dt.year + "-" + dt.month + "-" + dt.day + ", " + dt.hour +":" + dt.minute + ":" + dt.second);
}
file_get_date_modified
Get a datetime object representing the modification date of a particular file.
datetime file_get_date_modified(string filename);
Arguments:
- string filename; the name of the file to check.
Returns:
datetime: an initialized datetime object, with all its fields set to the modification date of the file.
Remarks:
The date returned is in UTC, not your local timezone.
Example:
void main() {
datetime dt = file_get_date_modified("file_get_date_modified.nvgt");
alert("file_get_date_modified.nvgt was modified on", dt.year + "-" + dt.month + "-" + dt.day + ", " + dt.hour +":" + dt.minute + ":" + dt.second);
}
file_get_size
Get the size of a file (in bytes).
int64 file_get_size(string filename);
Arguments:
- string filename: the name/path of the file to get the size of.
Returns:
int64: the size of the file (in bytes).
Example:
void main() {
alert("file_get.size.nvgt is", file_get_size("file_get_size.nvgt") + " bytes");
}
glob
Return a list of files and directories on the filesystem given a path glob.
string[]@ glob(string path_pattern, glob_options options = GLOB_DEFAULT);
Arguments:
string path_pattern: The pattern to match files and directories with (Unix shell like, see remarks).
glob_options options: A bitwise of glob_options enum constants that influence the behavior of this function (see remarks).
Returns:
string[]@: A list of all matching files and directories that match the given path pattern, an empty array on no matches or failure.
Remarks:
This function provides for one of the easiest ways to enumerate the filesystem in NVGT, particularly because the path patterns provided can actually cause semi-recursive directory searches. The search starts at the current working directory unless an absolute path is given.
The glob patterns have simple rules:
path separators must be matched exactly, * will not cause a recursive lookup
* matches any sequence of characters
? matches any single character
[set] matches any characters between the brackets
[!set] matches any characters that are not listed between the brackets
\*, \[, \] etc
exactly match a special character usually used as part of the glob expression
There is no guarantee that the items returned will appear in any particular order in the array.
The following glob_options constance are defined:
GLOB_DEFAULT: the default options
GLOB_IGNORE_HIDDEN: do not match when directory entries begin with a .
GLOB_FOLLOW_SYMLINKS: traverse even across symbolic links if the given pattern demands it
GLOB_CASELESS: match case insensitively
Example:
void main() {
// List all .nvgt files within the filesystem documentation directory.
string[]@ items = glob("../*/*.nvgt");
alert("files found", string(items));
}
Global Properties
DIRECTORY_APPDATA
Property that returns the user's roaming application directory, which is usually where game data can be written to.
const string DIRECTORY_APPDATA;
remarks:
A slash character is already appended to the directory returned by this property.
This function may return different values depending on the operating system the application is being run on.
On Windows, usually C:\Users%username%\appdata\roaming/.
on macOS, usually ~/Library/Preferences/.
on Linux, usually ~/.config/.
In any case, the directory returned should be writable.
Example:
void main() {
alert("example", "data for the game could be stored at " + DIRECTORY_APPDATA + "my_game/");
}