Folder Operations
Required version: Sponge >=1.0.7
Create, delete, and explore folders. The folder operations allow you to manage directory structures and list contents.
files.create_folder(path)
Creates a folder at the given path. Creates all parent directories if they don't exist.
Parameters:
path(string) - Path to the folder to create (e.g.,"data","src/utils","output/results")
Returns: null or error - Returns an error if the folder cannot be created
Example:
files.create_folder("mydata");Creating Nested Folders:
files.create_folder("project/src/components");
files.create_folder("project/src/utils");
files.create_folder("project/tests");Organizing Output:
files.create_folder("output");
files.write("output/results.txt", "Processing complete");
files.write("output/log.txt", "No errors");files.delete_folder(path, recursive?)
Deletes a folder. By default, only deletes empty folders. Set recursive to true to delete the folder and all contents.
Parameters:
path(string) - Path to the folder to deleterecursive(boolean, optional) - Iftrue, deletes folder and all contents. Default isfalse
Returns: null or error - Returns an error if the folder cannot be deleted
Example:
files.delete_folder("temp");Deleting Non-Empty Folders:
files.delete_folder("old_data", true);Cleanup on Exit:
if files.exists("temp_workspace") {
files.delete_folder("temp_workspace", true);
print("Cleaned up temporary files");
}files.list(path)
Lists all items (files and folders) in a folder.
Parameters:
path(string) - Path to the folder to list
Returns: array - Array of strings containing item names, or an error if the folder cannot be read
Example:
let items = files.list("mydata");
print(items);Processing All Files:
let files_list = files.list("documents");
for filename in files_list {
print("Found: " + filename);
}Counting Items:
let items = files.list("projects");
print("Total items: " + len(items));files.is_file(path)
Checks if the given path is a file.
Parameters:
path(string) - Path to check
Returns: boolean - true if the path is a file, false otherwise (or if path doesn't exist)
Example:
if files.is_file("data.txt") {
print("It's a file");
}Processing Specific Files:
let items = files.list("mydata");
for item in items {
if files.is_file("mydata/" + item) {
print("Processing file: " + item);
}
}files.is_folder(path)
Checks if the given path is a folder.
Parameters:
path(string) - Path to check
Returns: boolean - true if the path is a folder, false otherwise (or if path doesn't exist)
Example:
if files.is_folder("documents") {
print("It's a folder");
}Organizing by Type:
let items = files.list("mixed_content");
for item in items {
let full_path = "mixed_content/" + item;
if files.is_file(full_path) {
print("File: " + item);
} else if files.is_folder(full_path) {
print("Folder: " + item);
}
}