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";Directory Class Pattern
Section titled “Directory Class Pattern”openDirectory(options)
Section titled “openDirectory(options)”Returns a Directory instance for a directory subpath.
openDirectory({ path })Parameters
Section titled “Parameters”options
path- A string indicating the directory to open.
openFile(options)
Section titled “openFile(options)”Returns a File instance for a file subpath.
openFile({ path, mode })Parameters
Section titled “Parameters”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".
delete(path)
Section titled “delete(path)”Removes the file or directory specified by the path.
move(fromPath, toPath[, directory])
Section titled “move(fromPath, toPath[, directory])”Moves and/or renames a file or directory.
status(path)
Section titled “status(path)”Returns a status object with information about the file or directory at the specified path.
createDirectory(path)
Section titled “createDirectory(path)”Creates a directory at the specified path.
scan([path])
Section titled “scan([path])”Returns an iterator that enumerates the contents of a directory.
readLink(path)
Section titled “readLink(path)”Reads the target of a symbolic link.
readLink(path)Parameters
Section titled “Parameters”path - A string indicating the path to the symbolic link.
Return Value
Section titled “Return Value”A string representing the target of the symbolic link.
CheckPath(path)
Section titled “CheckPath(path)”Checks if a path is valid and accessible.
CheckPath(path)Parameters
Section titled “Parameters”path - A string indicating the path to check.
createLink(path, target)
Section titled “createLink(path, target)”Creates a symbolic link at path pointing to target.
createLink(path, target)Parameters
Section titled “Parameters”path - A string indicating where to create the link.
target- A string indicating the target of the link.
[Symbol.iterator]()
Section titled “[Symbol.iterator]()”Returns an iterator that yields entries in the directory.
[Symbol.iterator]()Directory Iterator Class
Section titled “Directory Iterator Class”constructor(directory[, path])
Section titled “constructor(directory[, path])”Creates a new directory iterator instance.
constructor(directory, path)Parameters
Section titled “Parameters”directory - The parent Directory instance.
path(optional) - The starting path for iteration.
next()
Section titled “next()”Returns the next entry in the directory.
next()Return Value
Section titled “Return Value”An object with done and value properties.
return()
Section titled “return()”Closes the iterator and releases resources.
return()Home Directory Object
Section titled “Home Directory Object”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 directoryconst config = files.openFile({ path: "config/settings.json" });File Class Pattern
Section titled “File Class Pattern”read(count, offset)
Section titled “read(count, offset)”Reads count bytes from the file starting at offset.
write(buffer, offset)
Section titled “write(buffer, offset)”Writes the content of buffer to the file starting at offset.
status()
Section titled “status()”Returns a status object for the file instance.
Properties
Section titled “Properties”
size- The length of the file in bytes.
mode- The file’s mode.
isFile()- Returnstrueif the path resolves to a file.
isDirectory()- Returnstrueif the path resolves to a directory.
flush()
Section titled “flush()”Ensures any data cached in memory for the file instance is persisted to storage.
close()
Section titled “close()”Releases all resources associated with the file or directory instance.
Examples
Section titled “Examples”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();