Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 29 Jun, 2020

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:

  • 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).

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.



  • 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.

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :