XDPXI's Documentation
Files

Examples

Required version: Sponge >=1.0.7

Simple File Operations

The most basic file read and write:

use files;

def main() {
    files.write("hello.txt", "Hello, World!");
    let content = files.read("hello.txt");
    print(content);
}

Configuration File Management

Load and save application settings:

use files;

def load_config() {
    if files.exists("config.txt") {
        return files.read("config.txt");
    } else {
        return "default=value";
    }
}

def save_config(config) {
    files.write("config.txt", config);
}

def main() {
    let config = load_config();
    print("Config: " + config);
    
    save_config("updated=value\nversion=2");
}

Data Logging

Build a simple logging system:

use files;

def log_event(event_type, message) {
    let timestamp = "2025-01-03";
    let log_line = "[" + timestamp + "] " + event_type + ": " + message + "\n";
    files.append("events.log", log_line);
}

def main() {
    log_event("INFO", "Application started");
    log_event("ACTION", "User logged in");
    log_event("INFO", "Processing complete");
}

Directory Organization

Create and organize folder structures:

use files;

def setup_project() {
    files.create_folder("src");
    files.create_folder("src/components");
    files.create_folder("src/utils");
    files.create_folder("tests");
    files.create_folder("output");
    
    files.write("src/main.sp", "def main() { print(\"Hello\"); }");
    files.write("README.md", "# My Project\n\nThis is my project.");
}

def main() {
    setup_project();
    print("Project structure created");
}

Batch File Processing

Process multiple files in a folder:

use files;

def process_folder(folder) {
    let items = files.list(folder);
    let file_count = 0;
    
    for item in items {
        let path = folder + "/" + item;
        if files.is_file(path) {
            let content = files.read(path);
            print("Processing: " + item + " (size: " + len(content) + ")");
            file_count = file_count + 1;
        }
    }
    
    print("Processed " + file_count + " files");
}

def main() {
    process_folder("documents");
}

File Backup

Create backups of important files:

use files;

def backup_file(original) {
    if files.exists(original) {
        let content = files.read(original);
        let backup = original + ".backup";
        files.write(backup, content);
        print("Backup created: " + backup);
    } else {
        print("File not found: " + original);
    }
}

def main() {
    backup_file("important_data.txt");
    backup_file("config.json");
}

Data Export

Collect data and export to file:

use files;

def export_users() {
    let users = [
        { id: 1, name: "Alice", email: "alice@example.com" },
        { id: 2, name: "Bob", email: "bob@example.com" },
        { id: 3, name: "Charlie", email: "charlie@example.com" }
    ];
    
    let csv = "id,name,email\n";
    for user in users {
        csv = csv + user["id"] + "," + user["name"] + "," + user["email"] + "\n";
    }
    
    files.write("users.csv", csv);
    print("Exported " + len(users) + " users to users.csv");
}

def main() {
    export_users();
}

Cleanup Temporary Files

Remove temporary files after processing:

use files;

def process_with_temp() {
    files.create_folder("temp");
    
    files.write("temp/data1.txt", "temporary data 1");
    files.write("temp/data2.txt", "temporary data 2");
    
    let items = files.list("temp");
    print("Processing " + len(items) + " temporary files");
    
    files.delete_folder("temp", true);
    print("Temporary files cleaned up");
}

def main() {
    process_with_temp();
}

Search and Replace in Files

Read, modify, and save file contents:

use files;

def replace_in_file(filepath, old_text, new_text) {
    let content = files.read(filepath);
    let updated = content;
    
    # Simple string replacement
    # Note: This is basic; for complex operations use more sophisticated methods
    let modified = files.read(filepath);
    
    files.write(filepath, modified);
    print("Updated: " + filepath);
}

def main() {
    files.write("document.txt", "Hello World\nHello Universe");
    let content = files.read("document.txt");
    print("Original:\n" + content);
}

Multi-File Operations

Combine content from multiple files:

use files;

def merge_files(folder, output) {
    let items = files.list(folder);
    let merged = "";
    
    for item in items {
        let path = folder + "/" + item;
        if files.is_file(path) {
            let content = files.read(path);
            merged = merged + "--- " + item + " ---\n";
            merged = merged + content + "\n\n";
        }
    }
    
    files.write(output, merged);
    print("Merged files saved to: " + output);
}

def main() {
    files.create_folder("parts");
    files.write("parts/intro.txt", "Introduction text here");
    files.write("parts/body.txt", "Main content here");
    files.write("parts/conclusion.txt", "Conclusion here");
    
    merge_files("parts", "combined.txt");
}

Safe File Operations with Error Handling

Handle file operation errors gracefully:

use files;

def safe_read(filepath) {
    if files.exists(filepath) {
        let content = files.read(filepath);
        if typeof(content) == "error" {
            print("Error reading: " + content);
            return "";
        }
        return content;
    } else {
        print("File not found: " + filepath);
        return "";
    }
}

def main() {
    let data = safe_read("data.txt");
    print("Read " + len(data) + " characters");
}

Project Initialization

Create a new project structure with template files:

use files;

def init_project(name) {
    files.create_folder(name);
    files.create_folder(name + "/src");
    files.create_folder(name + "/tests");
    files.create_folder(name + "/docs");
    
    files.write(name + "/README.md", "# " + name + "\n\nProject description here");
    files.write(name + "/src/main.sp", "def main() {\n    print(\"Hello from " + name + "\");\n}");
    files.write(name + "/.gitignore", "*.tmp\n*.log\n");
    
    print("Project initialized: " + name);
}

def main() {
    init_project("my_app");
}