Open In App

Node.js filehandle.stat() Method from class: FileHandle

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:



filehandle.stat(options);

Parameter: This method accepts single parameter as mentioned above and described below:

Return Value: It returns a resolved or rejected promise.promise is resolved with stats object if the directory is successfully read otherwise rejected with an error object if any error occurs(example-specified directory not exist or does not have permissions to read files, etc).



The stats object returned from the resolved promise has some properties and methods defined in it which helps in getting some specific details about the target files or folders.some of the methods are specifies below.

Example 1: This example differentiate files and folders of a directory using stats information of every sub-directory.




// 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}`)
        })
})

Output:

Example 2: This example counts size of each sub-directories using its stats information.




// 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}`)
        })  
})

Output:

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


Article Tags :