FSItemAn FSItem object represents a file or a directory in the file system of a Puter.
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.
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>
move()
Moves the item to another location.
Syntax
fsitem.move(destination)
fsitem.move(destination, overwrite)
fsitem.move(destination, overwrite, newName)
Parameters
destination (String) (required): The directory to move the item into, or the item's new path.overwrite (Boolean) (optional): Whether to overwrite an item that already exists at the destination. Defaults to false.newName (String) (optional): The name to give the item at its new location. Defaults to its current name.Return value
A Promise that resolves to the FSItem object of the moved item.
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('hello.txt', 'Hello, world!');
const item = await puter.fs.stat('hello.txt');
await item.move(dirname);
puter.print(`Moved hello.txt into ${dirname}`);
})();
</script>
</body>
</html>
copy()
Copies the item into another directory.
Syntax
fsitem.copy(destinationDirectory)
fsitem.copy(destinationDirectory, autoRename)
fsitem.copy(destinationDirectory, autoRename, overwrite)
Parameters
destinationDirectory (String) (required): The directory to copy the item into.autoRename (Boolean) (optional): Whether to pick a free name when an item with the same name already exists at the destination. Defaults to false, which makes the copy fail on a conflict.overwrite (Boolean) (optional): Whether to overwrite an item that already exists at the destination. Defaults to false.Return value
A Promise that resolves to the FSItem object of the copied item.
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('hello.txt', 'Hello, world!');
const item = await puter.fs.stat('hello.txt');
await item.copy(dirname);
puter.print(`Copied hello.txt into ${dirname}`);
})();
</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)
fsitem.mkdir(name, autoRename)
Parameters
name (String) (required): The name of the subdirectory to create.autoRename (Boolean) (optional): Whether to pick a free name when a directory with that name already exists. Defaults to false.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>