puter.fs.readdir()


Reads the contents of a directory, returning an array of items (files and directories) within it. This method is useful for listing all items in a specified directory in the Puter cloud storage.

Syntax

Code copied
puter.fs.readdir(path)

Parameters

path (string) The path to the directory to read. If path is not absolute, it will be resolved relative to the app's root directory.

Return value

A Promise that resolves to an array of fsitems (files and directories) within the specified directory.

Examples

Read a directory

Code copied
<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        puter.fs.readdir('./').then((items) => {
            // print the path of each item in the directory
            puter.print(`Items in the directory:<br>${items.map((item) => item.path)}<br>`);
        }).catch((error) => {
            puter.print(`Error reading directory: ${error}`);
        });
    </script>
</body>
</html>