FS
com.jsmacrosce.jsmacros.core.library.impl.FFS
File operations resolved against the running script's folder; the FS library is available to scripts as a global variable. Paths are not confined to that folder: absolute paths and .. components can refer elsewhere.
Use open(String) to read or write file contents; this library provides the path operations.
js
// Paths are relative to the script's folder.
if (!FS.exists("notes")) {
FS.createFile("notes", "readme.txt", true);
}
// A FileHandler reads and writes the file.
const handle = FS.open("notes/readme.txt");
handle.write("first line\n");
handle.append("second line\n");
print(handle.read());
const entries = FS.list("notes");
if (entries !== null) {
print(`notes holds ${JavaUtils.arrayToString(entries)}`);
}Accessible in scripts via the global FS variable.
Methods
combine
copy
createFile
Creates a new file in the specified path, relative to the script's folder. This will only work if the parent directory already exists. See createFile(String, String, boolean) to automatically create all parent directories.
Parameters:
path: the path relative to the script's foldername: the name of the file
Returns:
true if the file was created successfully, false otherwise.Creates an empty file by resolving
name under path. If createDirs is true, it attempts to create missing parent directories before creating the file. Existing files and directories are not replaced; creation returns false for either.Parameters:
path: the path relative to the script's foldername: the name of the filecreateDirs: whether to attempt to create missing parent directories
Returns: whether a new file was created
exists
getDir
getName
getRawAttributes
Reads the basic attributes of the path resolved against the script's folder.
Parameters:
path: path to read, resolved against the script's folder
Returns: the path's basic file attributes
isDir
isFile
list
makeDir
move
Moves the path
from to to; both paths are resolved against the script's folder. An existing destination is not replaced, and the move may fail if it exists.Parameters:
from: source path, resolved against the script's folderto: destination path, resolved against the script's folder
open
Returns a FileHandler for the path resolved against the script's folder, using the handler's default charset.
Parameters:
path: relative to the script's folder.
Returns: a handler for the resolved file
Returns a FileHandler for the path resolved against the script's folder, using the specified charset.
Parameters:
path: relative to the script's folder.charset: charset name used for reading and writing
Returns: a handler for the resolved file
toRawFile
toRawPath
toRelativePath
unlink
walkFiles
walkFiles(String path, int maxDepth, boolean followLinks, MethodWrapper<String, BasicFileAttributes, ?, ?> visitor): voidSince1.8.4#Walks a directory tree and calls
visitor for each encountered path with its basic attributes. Each callback path is computed as visitedPath.relativize(scriptFolder), yielding a path from the visited entry toward the script folder. maxDepth uses Files.walk semantics: the root is at depth zero. Attribute-read failures are printed and skipped; exceptions from the visitor propagate.Parameters:
path: path to the directory, resolved against the script's foldermaxDepth: maximum traversal depthfollowLinks: whether to follow symbolic linksvisitor: callback for each visited entry, with its path and attributes