Node.js filehandle.stat() Method from class: FileHandle
Last Updated :
29 Jun, 2020
Improve
The filehandle.stat() method is defined in File System module of Node.js. The File System module is basically to interact with the hard disk of the user's computers. The filehandle.stat() method gives some information specific to files and folders using methods defines on stats objects(object returned by stat method). The method returns a resolved or rejected promise.
Syntax:
javascript
Output:
Example 2: This example counts size of each sub-directories using its stats information.
javascript
Output:
Reference: https://nodejs.org/api/fs.html#fs_filehandle_stat_options
filehandle.stat(options);Parameter: This method accepts single parameter as mentioned above and described below:
- options: It is an optional parameter. One options parameter is ‘bigint’, it is a boolean value.here we specify if the numeric value in returned stats object by filehandle.stat() is biting or not(default-false).
- stats.isDirectory(): It returns true if stats object describes a file system directory.
- stats.isFile(): It returns true if stats object describes a regular file.
- stats.isSocket(): It returns true if stats object describes a socket.
- stats.isSymbolicLink(): It returns true if stats object describes a symbolic link.
- stats.isFile(): It returns true if stats object describes a regular file.
- stats.isFIFO(): It returns true if stats object describes first in first out pipe.
- stats.size: It specifies the size of the file in bytes.
- stats.blocks: It specifies the number of blocks allocated for the file.
// Node.js program to demonstrate the
// filehandle.stat() Method
// Importing File System and Utilities module
const fs = require('fs')
const fileOrFolder = async (dir) => {
let filehandle, stats = null
try {
filehandle = await fs
.promises.open(dir, mode = 'r+')
// Stats of directory
stats = await filehandle.stat()
} finally {
if (filehandle) {
// Close the file if it is opened.
await filehandle.close();
}
}
// File or Folder
if (stats.isFile()) {
console.log(`${dir} ----> File`)
} else {
console.log(`${dir} ----> Folder`)
}
}
const allDir = fs.readdirSync(process.cwd())
allDir.forEach(dir => {
fileOrFolder(dir)
.catch(err => {
console.log(`Error Occurs, Error code ->
${err.code}, Error NO -> ${err.errno}`)
})
})

// Node.js program to demonstrate the
// filehandle.stat() Method
// Importing File System and Utilities module
const fs = require('fs')
const sizeOfSubDirectory = async (dir) => {
let filehandle, stats = null
try {
filehandle = await fs
.promises.open(dir, mode = 'r+')
//Stats of directory
stats = await filehandle.stat()
} finally {
if (filehandle) {
// Close the file if it is opened.
await filehandle.close();
}
}
//size of sub-directory
console.log(`${dir} --------> ${stats.size} bytes`)
}
const allDir = fs.readdirSync(process.cwd())
allDir.forEach(dir => {
sizeOfSubDirectory(dir)
.catch(err => {
console.log(`Error Occurs, Error code ->
${err.code}, Error NO -> ${err.errno}`)
})
})
