Skip to content

Files

The Files module provides operations for files, directories, and links. The module’s default export is a Directory instance for the file system root.

import files from "embedded:storage/files";

Returns a Directory instance for a directory subpath.

openDirectory({ path })

options

path - A string indicating the directory to open.

Returns a File instance for a file subpath.

openFile({ path, mode })

options

path - A string indicating the name of the file to open.

mode - A string indicating the mode used to access the file. Values are "r", "r+", "w", and "w+". Defaults to "r".

Removes the file or directory specified by the path.

Moves and/or renames a file or directory.

Returns a status object with information about the file or directory at the specified path.

Creates a directory at the specified path.

Returns an iterator that enumerates the contents of a directory.

Reads the target of a symbolic link.

readLink(path)

path - A string indicating the path to the symbolic link.

A string representing the target of the symbolic link.

Checks if a path is valid and accessible.

CheckPath(path)

path - A string indicating the path to check.

Creates a symbolic link at path pointing to target.

createLink(path, target)

path - A string indicating where to create the link.

target - A string indicating the target of the link.

Returns an iterator that yields entries in the directory.

[Symbol.iterator]()

Creates a new directory iterator instance.

constructor(directory, path)

directory - The parent Directory instance.

path (optional) - The starting path for iteration.

Returns the next entry in the directory.

next()

An object with done and value properties.

Closes the iterator and releases resources.

return()

The Files module provides access to the home directory, which is the default directory for user files.

import files from "embedded:storage/files";
// Access files in the user's home directory
const config = files.openFile({ path: "config/settings.json" });

Reads count bytes from the file starting at offset.

Writes the content of buffer to the file starting at offset.

Returns a status object for the file instance.

size - The length of the file in bytes.

mode - The file’s mode.

isFile() - Returns true if the path resolves to a file.

isDirectory() - Returns true if the path resolves to a directory.

Ensures any data cached in memory for the file instance is persisted to storage.

Releases all resources associated with the file or directory instance.

import files from "embedded:storage/files";
const settings = files.openDirectory({ path: "settings" });
const wifi = settings.openFile({ path: "wifi.txt", mode: "r" });
const status = wifi.status();
const data = wifi.read(status.size, 0);
wifi.close();
settings.close();

Files