Node.js | fs.fstat() Method

The fs.fstat() method is used to return information about the given file descriptor. The fs.Stat object returned has several fields and methods to get more details about the file.

Syntax:

fs.fstat( fd, options, callback )

Parameters: This method accepts three parameters as mentioned above and described below:

  • fd: It is an integer which represents the file descriptor used by the method.
  • 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.fstat() method in Node.js:

Example 1: This example uses fs.fstat() method to get the details of a file and directory.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.fstat() method
  
// Import the filesystem module
const fs = require('fs');
  
// Define the file descriptor for a file
let file_fd = fs.openSync('example_file.txt', 'r');
  
// Getting information for a file
fs.fstat(file_fd, (error, stats) => {
  if (error) {
    console.log(error);
  }
  else {
    console.log("Stats object for: example_file.txt");
    console.log(stats);
  
    // Using methods of the Stats object
    console.log("Path is file:", stats.isFile());
    console.log("Path is directory:", stats.isDirectory());
  }
});
  
// Define the file descriptor for a folder
let dir_fd = fs.openSync('example_directory', 'r');
  
// Getting information for a directory
fs.fstat(dir_fd, (error, stats) => {
  if (error) {
    console.log(error);
  }
  else {
    console.log("Stats object for: example_directory.txt");
    console.log(stats);
  
    // Using methods of the Stats object
    console.log("Path is file:", stats.isFile());
    console.log("Path is directory:", stats.isDirectory());
  }
});

chevron_right


Output:

Stats object for: example_file.txt
Stats {
  dev: 3229478529,
  mode: 33206,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: 4096,
  ino: 281474976780635,
  size: 0,
  blocks: 0,
  atimeMs: 1584389463707.251,
  mtimeMs: 1582209885466.6848,
  ctimeMs: 1582209885466.6848,
  birthtimeMs: 1584389463707.251,
  atime: 2020-03-16T20:11:03.707Z,
  mtime: 2020-02-20T14:44:45.467Z,
  ctime: 2020-02-20T14:44:45.467Z,
  birthtime: 2020-03-16T20:11:03.707Z
}
Path is file: true
Path is directory: false
Stats object for: example_directory.txt
Stats {
  dev: 3229478529,
  mode: 16822,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: 4096,
  ino: 281474976780638,
  size: 0,
  blocks: 0,
  atimeMs: 1584429828080.8872,
  mtimeMs: 1581074249467.7114,
  ctimeMs: 1584389463715.2507,
  birthtimeMs: 1584389463715.2507,
  atime: 2020-03-17T07:23:48.081Z,
  mtime: 2020-02-07T11:17:29.468Z,
  ctime: 2020-03-16T20:11:03.715Z,
  birthtime: 2020-03-16T20:11:03.715Z
}
Path is file: false
Path is directory: true

Example 2: This example uses fs.fstat() method to get the details of a file with and without the bigint option.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// fs.fstat() method
  
// Import the filesystem module
const fs = require('fs');
  
// Define the file descriptor for a file
let file_fd = fs.openSync('example_file.txt', 'r');
  
fs.fstat(file_fd, (error, stats) => {
  console.log(stats);
});
  
// Using the bigint option to return
// the values in big integer format
fs.fstat(file_fd, { bigint: true }, (error, stats) => {
  console.log(stats);
});

chevron_right


Output:

Stats {
  dev: 3229478529,
  mode: 33206,
  nlink: 1,
  uid: 0,
  gid: 0,
  rdev: 0,
  blksize: 4096,
  ino: 281474976780635,
  size: 0,
  blocks: 0,
  atimeMs: 1584389463707.251,
  mtimeMs: 1582209885466.6848,
  ctimeMs: 1582209885466.6848,
  birthtimeMs: 1584389463707.251,
  atime: 2020-03-16T20:11:03.707Z,
  mtime: 2020-02-20T14:44:45.467Z,
  ctime: 2020-02-20T14:44:45.467Z,
  birthtime: 2020-03-16T20:11:03.707Z
}
BigIntStats {
  dev: 3229478529n,
  mode: 33206n,
  nlink: 1n,
  uid: 0n,
  gid: 0n,
  rdev: 0n,
  blksize: 4096n,
  ino: 281474976780635n,
  size: 0n,
  blocks: 0n,
  atimeMs: 1584389463707n,
  mtimeMs: 1582209885466n,
  ctimeMs: 1582209885466n,
  birthtimeMs: 1584389463707n,
  atimeNs: 1584389463707251000n,
  mtimeNs: 1582209885466684900n,
  ctimeNs: 1582209885466684900n,
  birthtimeNs: 1584389463707251000n,
  atime: 2020-03-16T20:11:03.707Z,
  mtime: 2020-02-20T14:44:45.466Z,
  ctime: 2020-02-20T14:44:45.466Z,
  birthtime: 2020-03-16T20:11:03.707Z
}

Reference: https://nodejs.org/api/fs.html#fs_fs_fstat_fd_options_callback




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.