Skip to content

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.

Classes

path

A path to a file or directory, which can be split into and built from its device, directories, filename and extension, and converted between the styles of different operating systems.

  1. path();

  2. path(bool absolute);

  3. path(const string&in path);

  4. path(const string&in path, spec::path_style style);

  5. path(const spec::path&in other);

  6. path(const spec::path&in parent, const string&in filename);

  7. path(const spec::path&in parent, const spec::path&in relative);

Arguments:

Remarks:

This class is in the spec namespace, so scripts write it as spec::path unless they have a using namespace spec; statement.

A path is made up of an optional node (for UNC paths on Windows), an optional device (such as a drive letter), a list of directories and an optional filename. A path whose string ends with a separator, such as sounds/, is a directory with no filename, while sounds/door is a file named door in the sounds directory, however it exists on disk. This class only deals with the text of paths, it never looks at the filesystem, so nothing has to exist.

Paths are parsed in the style of the operating system NVGT is running on unless a path_style is given, and to_string() can produce any style. On Windows both forward and back slashes are accepted.

A path converts to a string implicitly, and / joins two paths, so that spec::path("sounds") / "door.ogg" is sounds/door.ogg (or sounds\door.ogg on Windows). If the right side is absolute it is returned unchanged.

p[i] returns directory i, counting from 0, and an index equal to depth returns the filename.

Many methods return the path itself, so calls can be chained, such as p.make_directory().push_directory("music").

Example:

void main() {
	spec::path p("C:/Users/Sam/Documents/My Game/sounds/door_open.ogg", spec::PATH_STYLE_WINDOWS);
	string info = "device: " + p.device + "\ndepth: " + p.depth + "\nfilename: " + p.filename + "\nbasename: " + p.basename + "\nextension: " + p.extension + "\nparent: " + p.parent.to_string(spec::PATH_STYLE_WINDOWS);
	for (int i = 0; i < p.depth; i++) info += "\ndirectory " + i + ": " + p[i];
	p.set_extension("wav").pop_directory().push_directory("music");
	info += "\nchanged: " + p.to_string(spec::PATH_STYLE_UNIX);
	spec::path joined = spec::path("data") / "levels" / "1.json";
	info += "\njoined: " + joined;
	alert("Path", info);
}

Methods

absolute

Get an absolute version of the path, leaving this one unchanged.

  1. spec::path path::absolute() const;
  2. spec::path path::absolute(const spec::path&in base) const;
Arguments:
Returns:

spec::path: the absolute path, or a copy of this path if it is already absolute.

append

Add a relative path to the end of this one.

spec::path& path::append(const spec::path&in path);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

Remarks:

The / operator does the same without changing either path.

assign

Replace the path by parsing a string, or copying another path.

  1. spec::path& path::assign(const string&in path);
  2. spec::path& path::assign(const string&in path, spec::path_style style);
  3. spec::path& path::assign(const spec::path&in other);
Arguments:
Returns:

spec::path&: this path, so calls can be chained.

Remarks:

An exception is thrown if the string isn't a valid path. parse() returns false instead.

assign_directory

Replace the path with a directory, treating the string as a directory even if it doesn't end with a separator.

  1. spec::path& path::assign_directory(const string&in directory);
  2. spec::path& path::assign_directory(const string&in directory, spec::path_style style);
Arguments:
Returns:

spec::path&: this path, so calls can be chained.

clear

Clear every part of the path.

spec::path& path::clear();

Returns:

spec::path&: this path, so calls can be chained.

make_absolute

Make the path absolute by resolving it against a base path, if it is relative.

  1. spec::path& path::make_absolute();
  2. spec::path& path::make_absolute(const spec::path&in base);
Arguments:
Returns:

spec::path&: this path, so calls can be chained.

make_directory

Turn the path into a directory, moving its filename, if any, onto the end of its directories.

spec::path& path::make_directory();

Returns:

spec::path&: this path, so calls can be chained.

make_file

Turn the path into a file, moving its last directory, if any, into its filename.

spec::path& path::make_file();

Returns:

spec::path&: this path, so calls can be chained.

make_parent

Change the path to its parent directory.

spec::path& path::make_parent();

Returns:

spec::path&: this path, so calls can be chained.

Remarks:

A file becomes the directory it is in, and a directory loses its last directory. A relative path with no directories left gets a .. directory.

parse

Replace the path by parsing a string, without throwing an exception if it isn't valid.

  1. bool path::parse(const string&in path);
  2. bool path::parse(const string&in path, spec::path_style style);
Arguments:
Returns:

bool: true if the path was valid, false otherwise, in which case the path is unchanged.

pop_directory

Remove the last directory of the path.

spec::path& path::pop_directory();

Returns:

spec::path&: this path, so calls can be chained.

pop_front_directory

Remove the first directory of the path.

spec::path& path::pop_front_directory();

Returns:

spec::path&: this path, so calls can be chained.

push_directory

Add a directory to the end of the path's directories.

spec::path& path::push_directory(const string&in directory_name);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

resolve

Resolve a path against this one, which replaces this path if it is absolute, or is appended to it if it is relative.

spec::path& path::resolve(const spec::path&in path);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

set_basename

Set the filename of the path without changing its extension.

spec::path& path::set_basename(const string&in basename);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

set_device

Set the device of the path, such as a Windows drive letter.

spec::path& path::set_device(const string&in device);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

Remarks:

The current device can be read with the device property.

set_extension

Set the extension of the path's filename.

spec::path& path::set_extension(const string&in extension);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

set_filename

Set the filename of the path, including its extension.

spec::path& path::set_filename(const string&in filename);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

set_node

Set the node of the path, the server name in a Windows UNC path such as \\server\share.

spec::path& path::set_node(const string&in node);

Arguments:
Returns:

spec::path&: this path, so calls can be chained.

Remarks:

The current node can be read with the node property.

to_string

Get the path as a string in a particular style.

string path::to_string(spec::path_style style = spec::PATH_STYLE_NATIVE) const;

Arguments:
Returns:

string: the path in the given style.

Remarks:

Converting a path to a string implicitly, such as by concatenating it, uses the native style.

Properties

basename

The filename of the path without its extension, so door_open for door_open.ogg.

const string path::basename;

Remarks:

Use set_basename() to change it.

depth

The number of directories in the path, not counting the filename.

const int path::depth;

device

The device of the path, such as the drive letter of a Windows path, or an empty string.

const string path::device;

Remarks:

Use set_device() to change it.

extension

The extension of the path's filename without the dot, such as ogg, or an empty string if it has none.

const string path::extension;

Remarks:

Use set_extension() to change it.

filename

The filename of the path, including its extension, or an empty string for a directory.

const string path::filename;

Remarks:

Use set_filename() to change it.

is_absolute

Whether the path is absolute, such as C:\games or /home/sam.

const bool path::is_absolute;

is_directory

Whether the path refers to a directory, meaning it has no filename.

const bool path::is_directory;

Remarks:

This is about the text of the path only, so sounds/ is a directory and sounds is a file named sounds, whatever is on disk.

is_file

Whether the path refers to a file, meaning it has a filename.

const bool path::is_file;

Remarks:

This is about the text of the path only, see is_directory.

is_relative

Whether the path is relative, such as sounds/door.ogg, meaning it starts from some other directory.

const bool path::is_relative;

node

The node of the path, the server name of a Windows UNC path such as \\server\share, or an empty string.

const string path::node;

Remarks:

Use set_node() to change it.

parent

The parent directory of the path, as a new path.

const spec::path path::parent;

Remarks:

See make_parent(), which changes the path itself.

vms_version

The version of a file in an OpenVMS path, which is empty on every other system.

const string path::vms_version;

Enums

path_style

The styles a spec::path can be parsed and written in, in the spec namespace, so written as spec::PATH_STYLE_UNIX and so on.

Functions

directory_create

Creates a directory if it doesn't already exist.

bool directory_create(const string&in directory);

Arguments:

Returns:

bool: 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:

Returns:

bool: true if the directory was successfully deleted, false otherwise.

directory_exists

Query whether or not a directory exists.

bool directory_exists(const string&in directory);

Arguments:

Returns:

bool: 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(const string&in source_filename, const string&in destination_filename);

Arguments:

Returns:

bool: true if the file was successfully coppied, false otherwise.

file_delete

Deletes a file.

bool file_delete(const string&in filename);

Arguments:

Returns:

bool: true if the file was successfully deleted, false otherwise.

file_exists

Check for the existence of a particular file.

bool file_exists(const string&in file_path);

Arguments:

Returns:

bool: true if the file exists, false otherwise.

file_get_contents

Reads the entire content of a file and returns it as a string.

string file_get_contents(string filename)

Arguments:

Returns:

The contents of the file on success, an empty string on failure.

Remarks:

This is a convenience function, though it is currently the only way to read an Android asset. If you wish to access a file with more control, see the file datastream class.

Be careful to avoid reading files that are too large with this function, as the entire file will be in memory in order to return it as a continguous string.

Example:

void main() {
	string filename = input_box("Filename", "Enter the name of a file to read.", "");
	string contents = file_get_contents(filename);
	if (contents == "")
		alert("Example", "Either the file was not found, or it contained no text.");
	else {
		clipboard_set_text(contents);
		alert("Example", "The contents of the file is now on your clipboard.");
	}
}

file_get_date_created

Get a datetime object representing the creation date of a particular file.

datetime file_get_date_created(const string&in filename);

Arguments:

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(const string&in filename);

Arguments:

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(const string&in filename);

Arguments:

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");
}

file_put_contents

Writes a string to a file.

bool file_put_contents(string filename, string content, bool append = false)

Arguments:

Returns:

bool: true on success, false on failure.

Example:

void main() {
	if (!file_put_contents("example.txt", "This is an example"))
		alert("Example", "Failed to write the file.");
	else
		alert("Example", "Successfully wrote the example file.");
}

get_preferences_path

Determines the best location to store user data for your game, creating the directory for you if necessary.

string get_preferences_path(string company_name, string product_name);

Arguments:

Remarks:

This is the recommended way to determine where your game should store application data. While other properties exist such as DIRECTORY_APPDATA which can be used to compose your own path if you desire, this function is generally more robust and platform agnostic than trying to prepare your own storage path using directory constants. This also frees you from needing to check for the existance of the directory before writing to it.

The company name and product name should never change for an application once you start using them, lest players lose access to their previous save data due to the directory changing names. The company name can be blank if you wish to only create one level of new directories in the users appdata, though this is generally not recommended to avoid clutter.

Both the company and product name are expected to contain only spaces and alphanumeric characters if you wish to be as platform agnostic as possible.

You should not assume that DIRECTORY_APPDATA + "/" + company_name + "/" + product_name will be the same as the directory returned by this function, or even that both the company and product names will be used all the time. On android, for example, this function always just returns the app's internal storage path.

This function is also registered as the alias DIRECTORY_PREFERENCES, for those who prefer the old style of directory constants naming.

Example:

void main() {
	string path = get_preferences_path("", "documentation example");
	alert("test", "data will be stored at " + path);
	directory_delete(path); // In this example we don't actually want this directory to save.
}

glob

Return a list of files and directories on the filesystem given a path glob.

string[]@ glob(const string&in path_pattern, glob_options options = GLOB_DEFAULT);

Arguments:

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:

There is no guarantee that the items returned will appear in any particular order in the array.

The following glob_options constance are defined:

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.

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/");
}

DIRECTORY_COMMON_APPDATA

Property that returns the system wide application data directory, which is shared by every user on the computer.

const string DIRECTORY_COMMON_APPDATA;

remarks:

A slash character is already appended to the directory returned by this property.

This property may return different values depending on the operating system the application is being run on.

This directory is often not writable without administrator or root privileges, so it is mostly useful for reading configuration that was put there by an installer. Per user data should go in DIRECTORY_APPDATA or DIRECTORY_LOCAL_APPDATA instead.

Example:

void main() {
	alert("example", "shared configuration for the game could be read from " + DIRECTORY_COMMON_APPDATA + "my_game/");
}

DIRECTORY_HOME

Property that returns the current user's home directory.

const string DIRECTORY_HOME;

remarks:

A slash character is already appended to the directory returned by this property.

This property may return different values depending on the operating system the application is being run on.

Games should usually not write files directly into the home directory. Use DIRECTORY_APPDATA or DIRECTORY_LOCAL_APPDATA for game data instead.

Example:

void main() {
	alert("example", "Your home directory is " + DIRECTORY_HOME);
}

DIRECTORY_LOCAL_APPDATA

Property that returns the user's local application data directory, which is suitable for data that should stay on the current machine.

const string DIRECTORY_LOCAL_APPDATA;

remarks:

A slash character is already appended to the directory returned by this property.

This property may return different values depending on the operating system the application is being run on.

On Windows, the difference from DIRECTORY_APPDATA is that the roaming directory may be synchronized between computers on some networks, while the local directory never is. Large files or data that only makes sense on one computer, such as caches, belong in the local directory.

Example:

void main() {
	alert("example", "a cache for the game could be stored at " + DIRECTORY_LOCAL_APPDATA + "my_game/cache/");
}

DIRECTORY_TEMP

Property that returns the system's temporary directory, a place where short-term data can be stored.

const string DIRECTORY_TEMP;

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.

In any case the directory returned should be writable, though you should expect things you store there to be deleted by external factors at any time.

Example:

void main() {
	alert("example", "a temporary file for the game could be stored at " + DIRECTORY_TEMP + "filename.txt");
}