Skip to content

FS ​

com.jsmacrosce.jsmacros.core.library.impl.FFS

Since1.1.8

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 ​

combine(String patha, String pathb): StringSince1.1.8#
Combines path components using the platform's path syntax; this does not resolve them against the script's folder.

Parameters:

  • patha: first path component
  • pathb: second path component
Returns: the combined path

copy ​

copy(String from, String to): voidSince1.1.8#
Copies the path from to to; both paths are resolved against the script's folder. An existing destination is not overwritten.

Parameters:

  • from: source path, resolved against the script's folder
  • to: destination path, resolved against the script's folder

createFile ​

createFile(String path, String name): booleanSince1.8.4#
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 folder
  • name: the name of the file
Returns: true if the file was created successfully, false otherwise.
createFile(String path, String name, boolean createDirs): booleanSince1.8.4#
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 folder
  • name: the name of the file
  • createDirs: whether to attempt to create missing parent directories
Returns: whether a new file was created

exists ​

exists(String path): booleanSince1.1.8#
Checks whether a path exists.

Parameters:

  • path: relative to the script's folder.
Returns: whether the path exists

getDir ​

getDir(String path): StringSince1.1.8#
Returns the parent of the path resolved against the script's folder, expressed relative to that folder.

Parameters:

  • path: relative to the script's folder.
Returns: the parent path relative to the script's folder

getName ​

getName(String path): StringSince1.1.8#
Returns the final name component of the resolved path.

Parameters:

  • path: relative to the script's folder.
Returns: the final name component

getRawAttributes ​

getRawAttributes(String path): BasicFileAttributesSince1.8.4#
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 ​

isDir(String path): booleanSince1.1.8#
Checks whether a path is a directory.

Parameters:

  • path: relative to the script's folder.
Returns: whether the path is a directory

isFile ​

isFile(String path): booleanSince1.8.4#
Checks whether the path resolves to a regular file.

Parameters:

  • path: relative to the script's folder
Returns: whether the path is a regular file

list ​

list(String path): String[]Since1.1.8#
Lists the entries in a directory.

Parameters:

  • path: relative to the script's folder.
Returns: the entry names, or null if listing fails

makeDir ​

makeDir(String path): booleanSince1.1.8#
Creates one directory; its parent must already exist. Returns false if it already exists or creation fails.

Parameters:

  • path: relative to the script's folder.
Returns: whether the directory was created

move ​

move(String from, String to): voidSince1.1.8#
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 folder
  • to: destination path, resolved against the script's folder

open ​

open(String path): FileHandlerSince1.1.8#
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
open(String path, String charset): FileHandlerSince1.8.4#
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 ​

toRawFile(String path): FileSince1.8.4#
Returns a File for the path resolved against the script's folder.

Parameters:

  • path: path resolved against the script's folder
Returns: the resolved file

toRawPath ​

toRawPath(String path): PathSince1.8.4#
Returns the path resolved against the script's folder.

Parameters:

  • path: path resolved against the script's folder
Returns: the resolved path

toRelativePath ​

toRelativePath(String absolutePath): StringSince1.8.4#
Relativizes an absolute path against the script's folder.

Parameters:

  • absolutePath: absolute path to relativize
Returns: the path relative to the script's folder

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 folder
  • maxDepth: maximum traversal depth
  • followLinks: whether to follow symbolic links
  • visitor: callback for each visited entry, with its path and attributes