The fs.stat() method is used to return information about the given file or directory. It returns an fs.Stat object which has several properties and methods to get details about the file or directory.
Syntax:
fs.stat( path, options, callback )
Parameters: This method accept three parameters as mentioned above and described below:
- path: It holds the path of the file or directory that has to be checked. It can be a String, Buffer or URL.
- options: It is an object that can be used to specify optional parameters that will affect the output. It has one optional parameter:
- bigint: It is a boolean value which specifies if the numeric values returned in the fs.Stats object are bigint. The default value is false.
- callback: It is the function that would be called when the method is executed.
- err: It is an error that would be thrown if the method
- stats: It is the Stats object that contains the details of the file path.
Below examples illustrate the fs.stat() method in Node.js:
Example 1: This example uses fs.stat() method to get the details of the path.
const fs = require( 'fs' );
fs.stat( "example_file.txt" , (error, stats) => {
if (error) {
console.log(error);
}
else {
console.log( "Stats object for: example_file.txt" );
console.log(stats);
console.log( "Path is file:" , stats.isFile());
console.log( "Path is directory:" , stats.isDirectory());
}
});
fs.stat( "example_directory.txt" , (error, stats) => {
if (error) {
console.log(error);
}
else {
console.log( "Stats object for: example_directory.txt" );
console.log(stats);
console.log( "Path is file:" , stats.isFile());
console.log( "Path is directory:" , stats.isDirectory());
}
});
|
Output:
Stats object for: example_file.txt
Stats {
dev: 2269,
mode: 33188,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 271,
size: 0,
blocks: 0,
atimeMs: 1582871562365.894,
mtimeMs: 1582871556897.5554,
ctimeMs: 1582871556897.5554,
birthtimeMs: 1582871556897.5554,
atime: 2020-02-28T06:32:42.366Z,
mtime: 2020-02-28T06:32:36.898Z,
ctime: 2020-02-28T06:32:36.898Z,
birthtime: 2020-02-28T06:32:36.898Z }
Path is file: true
Path is directory: false
Stats object for: example_directory.txt
Stats {
dev: 2269,
mode: 33188,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 270,
size: 0,
blocks: 0,
atimeMs: 1582871562357.8936,
mtimeMs: 1582871553413.3396,
ctimeMs: 1582871553417.3398,
birthtimeMs: 1582871553413.3396,
atime: 2020-02-28T06:32:42.358Z,
mtime: 2020-02-28T06:32:33.413Z,
ctime: 2020-02-28T06:32:33.417Z,
birthtime: 2020-02-28T06:32:33.413Z }
Path is file: true
Path is directory: false
Example 2: This example uses fs.stat() method to get the details of files with the bigint option.
const fs = require( 'fs' );
fs.stat( "example_file.txt" , (error, stats) => {
console.log(stats);
});
fs.stat( "example_file.txt" , { bigint: true }, (error, stats) => {
console.log(stats);
});
|
Output:
Stats {
dev: 2269,
mode: 33188,
nlink: 1,
uid: 1000,
gid: 1000,
rdev: 0,
blksize: 4096,
ino: 271,
size: 0,
blocks: 0,
atimeMs: 1582871562365.894,
mtimeMs: 1582871556897.5554,
ctimeMs: 1582871556897.5554,
birthtimeMs: 1582871556897.5554,
atime: 2020-02-28T06:32:42.366Z,
mtime: 2020-02-28T06:32:36.898Z,
ctime: 2020-02-28T06:32:36.898Z,
birthtime: 2020-02-28T06:32:36.898Z }
Stats {
dev: 2269n,
mode: 33188n,
nlink: 1n,
uid: 1000n,
gid: 1000n,
rdev: 0n,
blksize: 4096n,
ino: 271n,
size: 0n,
blocks: 0n,
atimeMs: 1582871562365n,
mtimeMs: 1582871556897n,
ctimeMs: 1582871556897n,
birthtimeMs: 1582871556897n,
atime: 2020-02-28T06:32:42.365Z,
mtime: 2020-02-28T06:32:36.897Z,
ctime: 2020-02-28T06:32:36.897Z,
birthtime: 2020-02-28T06:32:36.897Z }
Reference: https://nodejs.org/api/fs.html#fs_fs_stat_path_options_callback