Modules@robinpath/fs
fs

@robinpath/fs

0.1.3Node.jsPublic

Read, write, copy, move, and manage files and directories

Fs

Read, write, copy, move, and manage files and directories

Package: @robinpath/fs | Category: Utility | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the fs module when you need to:

  • Read the contents of a file as a string -- Use fs.read to perform this operation
  • Write content to a file, creating or overwriting it -- Use fs.write to perform this operation
  • Append content to the end of a file -- Use fs.append to perform this operation
  • Check whether a file or directory exists at the given path -- Use fs.exists to perform this operation
  • Delete a file -- Use fs.delete to perform this operation

Quick Reference

FunctionDescriptionReturns
readRead the contents of a file as a stringThe file contents as a string
writeWrite content to a file, creating or overwriting itTrue if the write succeeded
appendAppend content to the end of a fileTrue if the append succeeded
existsCheck whether a file or directory exists at the given pathTrue if the path exists, false otherwise
deleteDelete a fileTrue if the file was deleted
copyCopy a file from source to destinationTrue if the copy succeeded
moveMove or rename a file from source to destinationTrue if the move succeeded
renameRename a file (alias for move)True if the rename succeeded
listList the contents of a directoryArray of filenames in the directory
mkdirCreate a directory (recursively creates parent directories)True if the directory was created
rmdirRemove a directory and its contentsTrue if the directory was removed
statGet file or directory statisticsObject with size, isFile, isDirectory, created, and modified properties
isFileCheck whether a path points to a regular fileTrue if the path is a regular file, false otherwise
isDirCheck whether a path points to a directoryTrue if the path is a directory, false otherwise

Functions

read

Read the contents of a file as a string

Module: fs | Returns: string -- The file contents as a string

any "/tmp/file.txt"
ParameterTypeRequiredDescription
pathstringYesAbsolute or relative path to the file
encodingstringNoCharacter encoding (default: utf-8)

write

Write content to a file, creating or overwriting it

Module: fs | Returns: boolean -- True if the write succeeded

any "/tmp/file.txt" "hello world"
ParameterTypeRequiredDescription
pathstringYesAbsolute or relative path to the file
contentstringYesThe content to write

append

Append content to the end of a file

Module: fs | Returns: boolean -- True if the append succeeded

any "/tmp/file.txt" "more text"
ParameterTypeRequiredDescription
pathstringYesAbsolute or relative path to the file
contentstringYesThe content to append

exists

Check whether a file or directory exists at the given path

Module: fs | Returns: boolean -- True if the path exists, false otherwise

any "/tmp/file.txt"
ParameterTypeRequiredDescription
pathstringYesAbsolute or relative path to check

delete

Delete a file

Module: fs | Returns: boolean -- True if the file was deleted

any "/tmp/file.txt"
ParameterTypeRequiredDescription
pathstringYesAbsolute or relative path to the file to delete

copy

Copy a file from source to destination

Module: fs | Returns: boolean -- True if the copy succeeded

any "/tmp/a.txt" "/tmp/b.txt"
ParameterTypeRequiredDescription
srcstringYesPath to the source file
deststringYesPath to the destination file

move

Move or rename a file from source to destination

Module: fs | Returns: boolean -- True if the move succeeded

any "/tmp/old.txt" "/tmp/new.txt"
ParameterTypeRequiredDescription
srcstringYesPath to the source file
deststringYesPath to the destination file

rename

Rename a file (alias for move)

Module: fs | Returns: boolean -- True if the rename succeeded

any "/tmp/old.txt" "/tmp/new.txt"
ParameterTypeRequiredDescription
oldPathstringYesCurrent file path
newPathstringYesNew file path

list

List the contents of a directory

Module: fs | Returns: array -- Array of filenames in the directory

any "/tmp"
ParameterTypeRequiredDescription
dirstringYesPath to the directory to list

mkdir

Create a directory (recursively creates parent directories)

Module: fs | Returns: boolean -- True if the directory was created

any "/tmp/my/nested/dir"
ParameterTypeRequiredDescription
pathstringYesPath of the directory to create

rmdir

Remove a directory and its contents

Module: fs | Returns: boolean -- True if the directory was removed

any "/tmp/my/dir"
ParameterTypeRequiredDescription
pathstringYesPath of the directory to remove

stat

Get file or directory statistics

Module: fs | Returns: object -- Object with size, isFile, isDirectory, created, and modified properties

any "/tmp/file.txt"
ParameterTypeRequiredDescription
pathstringYesPath to the file or directory

isFile

Check whether a path points to a regular file

Module: fs | Returns: boolean -- True if the path is a regular file, false otherwise

any "/tmp/file.txt"
ParameterTypeRequiredDescription
pathstringYesPath to check

isDir

Check whether a path points to a directory

Module: fs | Returns: boolean -- True if the path is a directory, false otherwise

any "/tmp"
ParameterTypeRequiredDescription
pathstringYesPath to check

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Validate result"
do
  set $result as any "/tmp/file.txt"
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Iterate results"
do
  set $result as any "/tmp"
  each $item in $result
    print $item
  end
enddo

2. Multi-step Fs workflow

Chain multiple fs operations together.

@desc "Execute operation"
do
  set $r_read as any "/tmp/file.txt"
  set $r_write as any "/tmp/file.txt" "hello world"
  set $r_append as any "/tmp/file.txt" "more text"
  print "All operations complete"
enddo

3. Safe read with validation

Check results before proceeding.

@desc "Validate result"
do
  set $result as any "/tmp/file.txt"
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (3)

VersionTagPublished
0.1.3latest21 days ago
0.1.221 days ago
0.1.11 months ago
Install
$ robinpath add @robinpath/fs

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.3
LicenseMIT
Unpacked Size9.3 KB
Versions3
Weekly Downloads26
Total Downloads26
Stars0
Last Publish21 days ago
Created1 months ago

Keywords

Category

utilities