# FS

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

**Since:** 1.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)](#com.jsmacrosce.jsmacros.core.library.impl.FFS_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 {#com.jsmacrosce.jsmacros.core.library.impl.FFS_combine-String-String-}

```js
combine(String patha, String pathb): String
```

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

**Since:** 1.1.8

### copy {#com.jsmacrosce.jsmacros.core.library.impl.FFS_copy-String-String-}

```js
copy(String from, String to): void
```

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

**Since:** 1.1.8

### createFile {#com.jsmacrosce.jsmacros.core.library.impl.FFS_createFile-String-String-}

```js
createFile(String path, String name): boolean
```

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)](#com.jsmacrosce.jsmacros.core.library.impl.FFS_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.

**Since:** 1.8.4

### createFile {#com.jsmacrosce.jsmacros.core.library.impl.FFS_createFile-String-String-boolean-}

```js
createFile(String path, String name, boolean createDirs): boolean
```

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

**Since:** 1.8.4

### exists {#com.jsmacrosce.jsmacros.core.library.impl.FFS_exists-String-}

```js
exists(String path): boolean
```

Checks whether a path exists.

**Parameters:**
- `path`: relative to the script's folder.

**Returns:** whether the path exists

**Since:** 1.1.8

### getDir {#com.jsmacrosce.jsmacros.core.library.impl.FFS_getDir-String-}

```js
getDir(String path): String
```

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

**Since:** 1.1.8

### getName {#com.jsmacrosce.jsmacros.core.library.impl.FFS_getName-String-}

```js
getName(String path): String
```

Returns the final name component of the resolved path.

**Parameters:**
- `path`: relative to the script's folder.

**Returns:** the final name component

**Since:** 1.1.8

### getRawAttributes {#com.jsmacrosce.jsmacros.core.library.impl.FFS_getRawAttributes-String-}

```js
getRawAttributes(String path): BasicFileAttributes
```

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

**Since:** 1.8.4

### isDir {#com.jsmacrosce.jsmacros.core.library.impl.FFS_isDir-String-}

```js
isDir(String path): boolean
```

Checks whether a path is a directory.

**Parameters:**
- `path`: relative to the script's folder.

**Returns:** whether the path is a directory

**Since:** 1.1.8

### isFile {#com.jsmacrosce.jsmacros.core.library.impl.FFS_isFile-String-}

```js
isFile(String path): boolean
```

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

**Since:** 1.8.4

### list {#com.jsmacrosce.jsmacros.core.library.impl.FFS_list-String-}

```js
list(String path): String[]
```

Lists the entries in a directory.

**Parameters:**
- `path`: relative to the script's folder.

**Returns:** the entry names, or `null` if listing fails

**Since:** 1.1.8

### makeDir {#com.jsmacrosce.jsmacros.core.library.impl.FFS_makeDir-String-}

```js
makeDir(String path): boolean
```

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

**Since:** 1.1.8

### move {#com.jsmacrosce.jsmacros.core.library.impl.FFS_move-String-String-}

```js
move(String from, String to): void
```

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

**Since:** 1.1.8

### open {#com.jsmacrosce.jsmacros.core.library.impl.FFS_open-String-}

```js
open(String path): FileHandler
```

Returns a [FileHandler](../../../../../../../classes/com/jsmacrosce/jsmacros/core/library/impl/classes/FileHandler.md) 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

**Since:** 1.1.8

### open {#com.jsmacrosce.jsmacros.core.library.impl.FFS_open-String-String-}

```js
open(String path, String charset): FileHandler
```

Returns a [FileHandler](../../../../../../../classes/com/jsmacrosce/jsmacros/core/library/impl/classes/FileHandler.md) 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

**Since:** 1.8.4

### toRawFile {#com.jsmacrosce.jsmacros.core.library.impl.FFS_toRawFile-String-}

```js
toRawFile(String path): File
```

Returns a [File](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/io/File.html) for the path resolved against the script's folder.

**Parameters:**
- `path`: path resolved against the script's folder

**Returns:** the resolved file

**Since:** 1.8.4

### toRawPath {#com.jsmacrosce.jsmacros.core.library.impl.FFS_toRawPath-String-}

```js
toRawPath(String path): Path
```

Returns the path resolved against the script's folder.

**Parameters:**
- `path`: path resolved against the script's folder

**Returns:** the resolved path

**Since:** 1.8.4

### toRelativePath {#com.jsmacrosce.jsmacros.core.library.impl.FFS_toRelativePath-String-}

```js
toRelativePath(String absolutePath): String
```

Relativizes an absolute path against the script's folder.

**Parameters:**
- `absolutePath`: absolute path to relativize

**Returns:** the path relative to the script's folder

**Since:** 1.8.4

### unlink {#com.jsmacrosce.jsmacros.core.library.impl.FFS_unlink-String-}

```js
unlink(String path): boolean
```

Attempts to delete the path resolved against the script's folder.

**Parameters:**
- `path`: relative to the script's folder.

**Returns:** whether deletion succeeded

**Since:** 1.2.9

### walkFiles {#com.jsmacrosce.jsmacros.core.library.impl.FFS_walkFiles-String-int-boolean-MethodWrapper-}

```js
walkFiles(String path, int maxDepth, boolean followLinks, MethodWrapper<String, BasicFileAttributes, ?, ?> visitor): void
```

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](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/nio/file/Files.html) 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

**Since:** 1.8.4
