File

File

File System utilities wrapped in a class

Constructor

new File(path)

Source:

Creates a new File object.

Parameters:
Name Type Description
path string

path to the file

Methods

(async) append(contents, optionsopt)

Source:

Append contents to the file.

Parameters:
Name Type Attributes Default Description
contents string | Buffer

contents to be written to the file

options object <optional>
{}

contains options for appending to the file

The options can include parameters such as fileMode, dirMode, retries and encoding.

(async) atime() → {Date}

Source:

Returns a Date object representing the time when file was last accessed.

Returns:

Date object, if file exists and its stats are read successfully; 0, otherwise

Type
Date

(async) chmod(mode) → {number}

Source:

Change the mode of the file.

Parameters:
Name Type Description
mode number | string

An octal number or a string representing the file mode

Returns:

0, on success; -1, if some error occurred

Type
number

(async) chmodr(mode) → {number}

Source:

Change the mode of the file or directory recursively.

Parameters:
Name Type Description
mode number | string

An octal number or a string representing the file mode

Returns:

0, on success; -1, if some error occurred

Type
number

(async) chown(user, group) → {number}

Source:

Change the owner and group of the file.

Parameters:
Name Type Description
user number | string

user id, or user name

group number | string

group id, or group name

Returns:

0, on success; any other number, if some error occurred

NOTE: If the owner or group is specified as -1, then that ID is not changed

Type
number

(async) chownr(user, group) → {number}

Source:

Change the owner and group of the file recursively.

Parameters:
Name Type Description
user number | string

user id, or user name

group number | string

group id, or group name

Returns:

0, on success; any other number, if some error occurred

NOTE: If the owner or group is specified as -1, then that ID is not changed

Type
number

(async) copy(destination, optionsopt)

Source:

Copy the file to some destination.

Parameters:
Name Type Attributes Default Description
destination string

path of the destination

options object <optional>
{}

options for copying the file

If the overwrite option is explicitly set to false, only then
will the function not attempt to overwrite the file if it (already)
exists at the destination.

(async) crtime() → {Date}

Source:

Returns a Date object representing the time when file was created.

Returns:

Date object, if file exists and its stats are read successfully; 0, otherwise

Type
Date

(async) ctime() → {Date}

Source:

Returns a Date object representing the time when file was last changed.

Returns:

Date object, if file exists and its stats are read successfully; 0, otherwise

Type
Date

(async) exists() → {boolean}

Source:

Checks whether a file exists already.

Returns:

true, if the file exists; false, otherwise

Type
boolean

existsSync() → {boolean}

Source:

Checks whether a file exists already.

Returns:

true, if the file exists; false, otherwise

Type
boolean

(async) glob() → {Array.<string>}

Source:

Perform a glob search with the path of the file as the pattern.

Returns:

Array containing the matches

Type
Array.<string>

(async) isDir() → {boolean}

Source:

Returns whether this File object represents a directory.

Returns:

true, if this object represents a directory; false, otherwise

Type
boolean

(async) isFile() → {boolean}

Source:

Returns whether this File object represents a file.

Returns:

true, if this object represents a file; false, otherwise

Type
boolean

(async) lstat() → {object}

Source:

Returns an object with the stats of the file. If the path for the file
is a symlink, then stats of the symlink are returned.

Returns:

Stats object

Type
object

(async) mkdir(modeopt)

Source:

Create a directory.

Parameters:
Name Type Attributes Default Description
mode number <optional>
0o755

file mode for the directory

(async) mkdirp(modeopt)

Source:

Create a new directory and any necessary subdirectories.

Parameters:
Name Type Attributes Default Description
mode number <optional>
0o755

file mode for the directory

(async) mkdirpPath(modeopt)

Source:

Create (all necessary directories for) the path of the file/directory.

Parameters:
Name Type Attributes Default Description
mode number <optional>
0o755

file mode for the directory

(async) mtime() → {Date}

Source:

Returns a Date object representing the time when file was last modified.

Returns:

Date object, if file exists and its stats are read successfully; 0, otherwise

Type
Date

(async) mv(newName) → {number}

Source:

Move file to a new location

Parameters:
Name Type Description
newName string

new location (or path) for the file

Returns:

0, on success; -1, if some error occurred

Type
number

(async) read() → {string|Buffer}

Source:

Read contents of the file.

Returns:

contents of the file

Type
string | Buffer

(async) realpath() → {string}

Source:

Return the canonicalized absolute pathname

Returns:

the resolved path

Type
string

realpathSync() → {string}

Source:

Return the canonicalized absolute pathname

Returns:

the resolved path

Type
string

(async) rename(newName) → {number}

Source:

Change the name or location of the file.

Parameters:
Name Type Description
newName string

new path/location (not just name) for the file

Returns:

0, on success; -1, if some error occurred

Type
number

(async) rm()

Source:

Remove the file.

NOTE: The path is unlinked from the file, but the file
is deleted only if the path was the only link to the file and
the file was not opened in any other process.

(async) rmdir()

Source:

Remove the directory.

NOTE: The directory will be deleted only if it is empty.

(async) rmrf()

Source:

Recursively delete the directory and all its contents.

(async) size() → {number}

Source:

Returns the size of the file in bytes. If the file is not found
or can't be read successfully, 0 is returned.

Returns:

Size of file (in bytes)

Type
number

(async) stat() → {object}

Source:

Returns an object with the stats of the file. If the path for the file
is a symlink, then stats of the target of the symlink are returned.

Returns:

Stats object

Type
object
Source:

Unlink the path from the file.

NOTE: If the path referred to a
symbolic link, the link is removed. If the path is the only link
to the file then the file will be deleted.

(async) write(contents, optionsopt)

Source:

Write contents to the file.

Parameters:
Name Type Attributes Default Description
contents string | Buffer

contents to be written to the file

options object <optional>
{}

contains options for writing to the file
The options can include parameters such as fileMode, dirMode, retries and encoding.