← coderrocketfuel.com

List All of the Files in a Directory Using Node.js

In this article, we will cover how to get all the files that are present in a directory and get their stats. We will use the Node.js fs and path core modules to achieve this.

We will use the three methods listed below:

  • path.join(): used to create a full file path for the directory you wish to get a list of files for.
  • fs.readdir(directoryPath, callback): used to read all the files in your directory. The function takes a file path to your directory and a callback function that returns a list of your files.
  • fs.stat(filePath, callback): takes a path to a file and returns an object with a bunch of stats about it.

Let's get started!

Table Of Contents

Get List of Files in the Directory

Below is the full code to get a list of files in a directory. We'll explain everything afterward.

const path = require("path")
const fs = require("fs")

const directoryPath = path.join(__dirname, "files")

fs.readdir(directoryPath, function(err, files) {
  if (err) {
    console.log("Error getting directory information.")
  } else {
    files.forEach(function(file) {
      console.log(file)
    })
  }
})

There are a couple of different pieces to explain here, so let's cover each part:

  1. Require both the path and fs Node.js core modules.
  2. Get the file path of your directory using the path.join() method.
  3. Use the fs.readdir() method to get all the files in the directory. Notice the directoryPath variable is passed to the function and all the files are returned in the callback.
  4. Some error handling is added inside the callback function.
  5. After the error handling, we run the forEach method on the array of files list from the directory.
  6. Inside the forEach function, we console.log() each file in the files array.

Get Stats for Each File in the Directory

In the last step, we listed out each file in the directory. But now let's get the stats for each file that the fs.readdir() method returns in its callback.

As an example, we'll use the fs.stat() method on each file in the forEach function.

Here's the full code:

const path = require("path")
const fs = require("fs")

const directoryPath = path.join(__dirname, "files")

fs.readdir(directoryPath, function(err, files) {
  if (err) {
    console.log("Error getting directory information.")
  } else {
    files.forEach(function(file) {
      fs.stat(directoryPath + `/${file}`, function(err, stats) {
        console.log(stats)
      })
    })
  }
})

Inside the forEach() function, we use the fs.stat() function on each file and pass the full file path of each file to it.

The fs.stat() function returns an object of stats for each file. When the code is run, the output should look similar to this in your terminal:

Stats {
  dev: 2066,
  mode: 33204,
  nlink: 1,
  uid: 1000,
  gid: 1000,
  rdev: 0,
  blksize: 4096,
  ino: 12206290,
  size: 197367,
  blocks: 400,
  atimeMs: 1564782358746.4778,
  mtimeMs: 1564733307250.1096,
  ctimeMs: 1564782008322.4856,
  birthtimeMs: 1564782008322.4856,
  atime: 2019-08-02T21:45:58.746Z,
  mtime: 2019-08-02T08:08:27.250Z,
  ctime: 2019-08-02T21:40:08.322Z,
  birthtime: 2019-08-02T21:40:08.322Z
}

You can read about all the items in that object in the official fs documentation.