Show all files and subdirectories with recursive open-dir


This example will show recursive use of open-dir to get all the files and subdirectories, and all the files and subdirectories within each subdirectory and so on.

First create a directory for this application (no need to do that of course if this is a part of larger application!):
mkdir -p dir
cd dir

Create "lsdir" application:
rim -k lsdir

Here's the code to save to file "dir.rim". It's quite simple: "path" is the directory path to list (relative to current directory), "indent" is the number of spaces to print before each entry in it (by default 0). The service will use read-dir to obtain each directory entry, and if it's a directory, it will call the service recursively. That's it:
cat << 'EOF' > dir.rim
begin-handler /dir public get-param path type string, indent type number default-value 0
    // Get current directory where command-line program executes
    get-sys directory to cdir
    // Add the path to it where we want to list directory entries, and open directory 
    open-dir cdir+"/"+path dir-id d
    // In a loop, read all directory entries
    start-loop
        read-dir d   to e status s type t
        if-true s not-equal RIM_OKAY
            break-loop // exit the loop if no more
        end-if
        if-true t not-equal RIM_UNKNOWN
            // Skip . and .. 
            if-true e not-equal "." and e not-equal ".."
                // Print space indentation for a nicer output
                call-handler "/dir/show-indent" set-param indent
                // Print the entry (file, directory, etc.)
                if-true t equal RIM_FILE
                    print-out "File: "+e new-line
                else-if t equal RIM_DIR
                    print-out "Dir: "+e new-line
                    // If directory, call this service again with the path, and 
                    // make it indented
                    call-handler "/dir" set-param path=path+"/"+e, indent=indent+4
                else-if
                    print-out "Other:"+e new-line
                end-if
            end-if
        end-if
    end-loop
    close-dir d // close each directory (without it, RimStone would do it automatically)
end-handler

// A very simple service to output a number of spaces
begin-handler /dir/show-indent get-param indent type number
    start-loop repeat indent
        print-out " "
    end-loop
end-handler
EOF

Build your application as a native executable:
rim -q

Create a test directory here, with subdirectories and some files:
mkdir -p dirtest
mkdir -p dirtest/dir1
mkdir -p dirtest/dir2
mkdir -p dirtest/dir2/dir3
echo "hi"> dirtest/dir2/dir3/file3
echo "hi"> dirtest/dir2/file2
echo "hi"> dirtest/file

Run it from command line:
rim -r --req="/dir/path=dirtest" --exec --silent-header

The result is:
File: file
Dir: dir1
Dir: dir2
    Dir: dir3
        File: file3
    File: file2

You can specify any relative path by using %2F (a URL encoding for forward slash), for instance to list the parent directory (which is "../" or "..%2F" in URL encoding):
rim -r --req="/dir/path=..%2F" --exec --silent-header

This example shows how to traverse directories programmatically. Of course, you can examine each entry for its type (such as sockets, fifo files, devices etc.) - this example shows how to deal with them as well.


Copyright (c) 2019-2025 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.