SultanC
  1. Home
  2. Examples
  3. Files

Creating nested directories, listing and walking them, then removing the tree

fs/directories.sn
// Creating nested directories, listing and walking them, then removing the tree.
import std.io.println;
import std.text.number;
import std.fs.walk;
import std.fs.directory;
import std.fs.write_file;

def main(): int {
    let root: string = "sultanc-example-tree";
    let nested: string = "sultanc-example-tree/a/b";
    let file: string = "sultanc-example-tree/a/note.txt";
    let content: string = "note";
    match create_dir_all(&nested) with
    | result.err(code) -> { return code; }
    | result.ok(_) -> { };
    match write_file(&file, &content) with
    | result.err(code) -> { return code; }
    | result.ok(_) -> { };
    let middle: string = "sultanc-example-tree/a";
    match list_directory(&middle) with
    | result.ok(entries) -> { println(uint_to_text(length(entries) as uint)); }
    | result.err(code) -> { return code; };
    match walk(&root) with
    | result.ok(all_entries) -> { println(uint_to_text(length(all_entries) as uint)); }
    | result.err(code) -> { return code; };
    match remove_tree(&root) with
    | result.ok(_) -> { println("removed"); }
    | result.err(code) -> { return code; };
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/fs/directories.sn

Or compile it to an executable:

./build/sultanc examples/en/fs/directories.sn -o build/example && ./build/example

Source on GitHub   All examples