FSItem


An FSItem object represents a file or a directory in the file system of a Puter.

Attributes

id (String)

A string containing the unique identifier of the item. This is a unique identifier generated by Puter when the item is created.

name (String)

A string containing the name of the item.

path (String)

A string containing the path of the item. This is the path of the item relative to the root directory of the file system.

isDir (Boolean)

A boolean value indicating whether the item is a directory. If this is set to true, the item is a directory. If this is set to false, the item is a file.

created (Integer)

An integer containing the Unix timestamp of the date and time when the item was created.

modified (Integer)

An integer containing the Unix timestamp of the date and time when the item was last modified.

accessed (Integer)

An integer containing the Unix timestamp of the date and time when the item was last accessed.

size (Integer)

An integer containing the size of the item in bytes. If the item is a directory, this will be null.

Methods

read()

Reads the contents of the file.

Syntax

fsitem.read()

Parameters

None.

Return value

A Promise that resolves to a Blob containing the contents of the file.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            const blob = await item.read();
            puter.print(await blob.text());
        })();
    </script>
</body>
</html>

write()

Writes data to the file, overwriting its existing contents.

Syntax

fsitem.write(data)

Parameters

  • data (String | File | Blob) (required): The data to write to the file.

Return value

A Promise that resolves to the FSItem object of the written file.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.write('Updated contents!');
            puter.print('File updated');
        })();
    </script>
</body>
</html>

rename()

Renames the item.

Syntax

fsitem.rename(newName)

Parameters

  • newName (String) (required): The new name for the item.

Return value

A Promise that resolves to the FSItem object of the renamed item.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.rename('renamed.txt');
            puter.print('File renamed');
        })();
    </script>
</body>
</html>

delete()

Deletes the item.

Syntax

fsitem.delete()

Parameters

None.

Return value

A Promise that resolves once the item has been deleted.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            await puter.fs.write('hello.txt', 'Hello, world!');
            const item = await puter.fs.stat('hello.txt');
            await item.delete();
            puter.print('File deleted');
        })();
    </script>
</body>
</html>

mkdir()

Creates a new subdirectory inside the item. The item must be a directory, otherwise an error is thrown.

Syntax

fsitem.mkdir(name)

Parameters

  • name (String) (required): The name of the subdirectory to create.

Return value

A Promise that resolves to the FSItem object of the created directory.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dirname = puter.randName();
            await puter.fs.mkdir(dirname);
            const dir = await puter.fs.stat(dirname);
            await dir.mkdir('subdir');
            puter.print(`Created subdir inside ${dirname}`);
        })();
    </script>
</body>
</html>

readdir()

Lists the contents of the item. The item must be a directory, otherwise an error is thrown.

Syntax

fsitem.readdir()

Parameters

None.

Return value

A Promise that resolves to an array of FSItem objects, one for each item in the directory.

Example

<html>
<body>
    <script src="https://js.puter.com/v2/"></script>
    <script>
        (async () => {
            const dirname = puter.randName();
            await puter.fs.mkdir(dirname);
            await puter.fs.write(dirname + '/hello.txt', 'Hello, world!');
            const dir = await puter.fs.stat(dirname);
            const children = await dir.readdir();
            children.forEach((child) => puter.print(child.name + '<br>'));
        })();
    </script>
</body>
</html>