> For the complete documentation index, see [llms.txt](https://openjs.wiki.gd/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://openjs.wiki.gd/apis-and-tools/services/filemanager.md).

# FileManager

## Overview:

<table data-header-hidden><thead><tr><th>Functions:</th></tr></thead><tbody><tr><td>Functions:</td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">fileExists(path) ⇒ boolean
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">read(path) ⇒ string
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">write(path, data) ⇒ ResultMap
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">createFile(path) ⇒ File
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">createFolder(path) ⇒ ResultMap
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">getFiles(path) ⇒ List [File or Folder]
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">getFile(path) ⇒ File
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">getFolder(path) ⇒ File
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">getPath(file) ⇒ path [String]
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">removeFile(path) ⇒ boolean
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">removeFolder(path) ⇒ boolean
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">listenOnPath(path, listenerType, function)
</code></pre></td></tr><tr><td><pre class="language-javascript"><code class="lang-javascript">clearFileListeners()
</code></pre></td></tr></tbody></table>

{% hint style="info" %}
The working directory is automatically the parent folder of the current script!
{% endhint %}

### API Reference

### `fileExists()`

Returns true if the file exists or false if it does not exist or is inaccessible.

```js
fileManager.fileExists("config.json")
```

**Parameters:**

* `path` *(String)*

**Returns** `Boolean`.

***

### `read()`

Returns the data of a readable file (in String) if successfully ran.

```js
let data = fileManager.read("config.json")
```

**Parameters:**

* `path` *(String)*

**Returns** `String`.

***

### `write()`

Writes the data (in String) on the file, overwriting its previous data.

```js
fileManager.write("config.json", "{}")
```

**Parameters:**

* `path` *(String)*
* `data` *(String)* ⇒ The content that will be written to the file

**Returns:**

```js
{success, error} => {"Success" | "Error", true | "Error reason"}
```

***

### `createFile()`

Creates a file (if it does not exist) on the specified path. Use forward slashes **`"/"`** to jump to folders.

```js
fileManager.createFile("config.json")
```

**Parameters:**

* `path` *(String)*

**Returns:**  [`java.io.File`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html)

***

### `createFolder()`

Creates a folder (if it does not exist) on the specified path. Use forward slashes **`"/"`** to jump to folders.

```js
fileManager.createFolder("Configuration")
```

**Parameters:**

* `path` *(String)*

**Returns:**

```js
{success, error} => {"Success" | "Error", true | "Error reason"}
```

***

### `getFiles()`

Returns a list of all files within the specified path.

```js
FileManager.getFiles("/");
```

**Parameters:**

* `path` *(String)*

**Returns:**

```js
[Files]
```

**Example usage:**

<pre class="language-javascript" data-title="Print all files that are in the current folder of the script"><code class="lang-javascript">const fileManager = Services.get("FileManager");
<strong>const files = fileManager.getFiles("/");
</strong>
for (let file of files) {
    print("Found file: " + file.getName());
}
</code></pre>

***

### `getFile()`

Returns a file (if it does exist) on the specified path.

```js
fileManager.getFile("config.json")
```

**Parameters:**

* `path` *(String)*

**Returns:**  [`java.io.File`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html)

***

### `getFolder()`

Returns a folder (if it does exist) on the specified path.

```js
fileManager.getFolder("configs")
```

**Parameters:**

* `path` *(String)*

**Returns:**  [`java.io.File`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html)

***

### `getPath()`

Returns a path that points towards the file.

```js
fileManager.getPath(File)
```

**Parameters:**

* `file` [`java.io.File`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html)

**Returns:**  `string` *(relative-path)*

***

### `removeFile()`

Removes the file.

```js
fileManager.removeFile("example.txt")
```

**Parameters:**

* `path` *(String)*

**Returns:**  `Boolean`

***

### `removeFolder()`

Removes the folder.

```js
fileManager.removeFolder("example")
```

**Parameters:**

* `path` *(String)*

**Returns:**  `Boolean`

***

### `listenOnPath()`

Allows scripts to listen for file changes on the specified path.

```js
fileManager.listenOnPath("ExampleFolder", "ENTRY_CREATE", function(File, EventType) {
    log.info(File.getName())
    log.info(EventType)
})
```

**Parameters:**

* `path` *(String)*
* `listenerType` *(String)*\
  *⇒* `"ENTRY_CREATE"` When a file gets added/created\
  \&#xNAN;*⇒* `"ENTRY_DELETE"` When a file gets removed/moved\
  \&#xNAN;*⇒* `"ENTRY_MODIFY"` When a file gets modified/edited

**Returns** <sup><sub>(function handler)<sub></sup>**:**

* File [`java.io.File`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/io/File.html)
* Event Type `"ENTRY_CREATE"` or `"ENTRY_DELETE"` or `"ENTRY_MODIFY"`

***

### `clearFileListeners()`

Removes all of the file listeners created by the current script.

```js
fileManager.clearFileListeners()
```

***

## Example:

{% code title="Create folder with listener example" %}

```js
let FileManager = Services.get("FileManager");
let FolderName = "SomeFiles"

FileManager.createFolder(FolderName)
FileManager.listenOnPath(FolderName, "ENTRY_CREATE", function(File) {
    log.info(File.getName() + " has been created")
})
FileManager.listenOnPath(FolderName, "ENTRY_DELETE", function(File) {
    log.info(File.getName() + " has been removed")
})
FileManager.listenOnPath(FolderName, "ENTRY_MODIFY", function(File) {
    log.info(File.getName() + " has been changed")
    log.info("It contains the following data: " + FileManager.read(FolderName + "/" + File.getName()))
})
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://openjs.wiki.gd/apis-and-tools/services/filemanager.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
