← coderrocketfuel.com

Get The Number Of Files In A Directory Using Node.js

How do you get the number of files in a directory or folder using Node.js?

Node.js has a built-in Fs core module that provides an fs.readdirSync() function that reads the contents of a directory at a given file path. Using that function, we can recursively go through each subdirectory and create a list that contains all the files for the directory.

To get the code in this article to work, make sure you have Node.js installed on your local machine.

If you need one, we created a guide on installing Node.js.

Let's get started!

We'll give you the full code to create a list of files and explain each part afterward.

Here's the code:

const fs = require("fs")

const getAllDirFiles = function(dirPath, arrayOfFiles) {
  files = fs.readdirSync(dirPath)

  arrayOfFiles = arrayOfFiles || []

  files.forEach(function(file) {
    if (fs.statSync(dirPath + "/" + file).isDirectory()) {
      arrayOfFiles = getAllDirFiles(dirPath + "/" + file, arrayOfFiles)
    } else {
      arrayOfFiles.push(file)
    }
  })

  return arrayOfFiles
}

Let's go over each part of the code.

The getAllDirFiles variable holds the recursive function that will go through each subdirectory and return an array of filenames. It takes a directory file path and an optional arrayOfFiles as arguments.

Inside the getAllDirFiles function, we first use the readdirSync() function to get all of the files and directories inside the given dirPath supplied to the function.

Then, we create an arrayOfFiles array that holds all the filenames that will be returned when the function is done running. Files will be appended to this array while each directory and sub-directory is looped over.

Next, we loop over each item (file or directory) found by the readdirSync() function. If the item is a directory, we have the function recursively call itself to get all of the files and subdirectories inside the given directory.

And if the item is a file, we append the filename to the arrayOfFiles array.

When the forEach loop has finished, we return the arrayOfFiles array.

Here is how you use the function in your code:

const result = getAllDirFiles("PATH_TO_PARENT_DIRECTORY")
// Output: [ "file.jpg", "file.txt", "file.txt" ]

And you can simply get the length of that array if you wanted to know how many files were found:

const result = getAllDirFiles("PATH_TO_PARENT_DIRECTORY").length
// Output: 3

When you log the value, an integer representing the number of files in the directory will be returned.

Here's the full code for your reference:

const fs = require("fs")

const getAllDirFiles = function(dirPath, arrayOfFiles) {
  files = fs.readdirSync(dirPath)

  arrayOfFiles = arrayOfFiles || []

  files.forEach(function(file) {
    if (fs.statSync(dirPath + "/" + file).isDirectory()) {
      arrayOfFiles = getAllDirFiles(dirPath + "/" + file, arrayOfFiles)
    } else {
      arrayOfFiles.push(file)
    }
  })

  return arrayOfFiles
}

const result = getAllDirFiles("PATH_TO_PARENT_DIRECTORY").length
// Output: 3

Thanks for reading and happy coding!