Filesystem¶
Provides access to the server’s world folder for managing files and directories.
To create, write, and read files use Lua's standard io module:
local advancements = spell.server.filesystem:list("advancements")
for i,filename in ipairs(advancements) do
local filepath = "advancements/"..filename
local f = io.open(filepath, "r")
if f then
local text = f:read("*a")
print(filename, text)
f:close()
end
end
Methods¶
list(path: string|nil) → files: string[]¶
Returns a list of file and directory names in the specified directory.
If path is omitted or nil, lists the root directory.
Example: List contents of a directory
local files = spell.server.filesystem:list("region")
for _, name in ipairs(files) do
print(name)
end
Example: List root directory
Parameters
| Name | Type | Description |
|---|---|---|
path |
string|nil |
Optional. Directory path to list. Lists root if omitted. |
Returns
string[]— files List of file and directory names.
isDir(path: string) → result: boolean¶
Checks if the given path is a directory.
Example
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path to check. |
Returns
boolean— result True if path is a directory, false otherwise.
isFile(path: string) → result: boolean¶
Checks if the given path is a regular file.
Example
if spell.server.filesystem:isFile("generated/minecraft/structures/arena.nbt") then
print("Arena structure exists.")
end
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path to check. |
Returns
boolean— result True if path is a file, false otherwise.
makeDir(path: string) → result: boolean¶
Creates a directory at the given path.
Example
if spell.server.filesystem:makeDir("backups") then
print("Directory created.")
else
print("Failed to create directory.")
end
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Directory path to create. |
Returns
boolean— result True if successful, false otherwise.
delete(path: string) → result: boolean¶
Deletes a file or an empty directory. The directory must be empty to be deleted.
Example
if spell.server.filesystem:delete("backups/old") then
print("Deleted successfully.")
else
print("Delete failed.")
end
Parameters
| Name | Type | Description |
|---|---|---|
path |
string |
Path to the file or directory. |
Returns
boolean— result True if successful, false otherwise.
move(fromPath: string, toPath: string) → result: boolean¶
Moves or renames a file or directory.
Example
if spell.server.filesystem:move(
"generated/minecraft/structures/arena.nbt",
"archive/arena.nbt")
then
print("File moved to archive.")
else
print("Move failed.")
end
Parameters
| Name | Type | Description |
|---|---|---|
fromPath |
string |
Current path. |
toPath |
string |
New path. |
Returns
boolean— result True if successful, false otherwise.