Last updated: Apr 5, 2024
Reading timeยท4 min
Note: if you need to count the number of files in a directory
recursively
, click on the following subheading:
To count the number of files in a directory:
fs.readdir()
method to read the contents of the directory.length
property on the returned array of files to get the number
of files in the directory.import fs from 'fs'; // ๐๏ธ if you use the CommonJS require() syntax // const fs = require('fs') const pathToDirectory = './my-folder'; fs.readdir(pathToDirectory, (error, files) => { if (error) { console.log(error); } else { console.log(files); console.log(files.length); } });
Make sure to specify the correct path to the directory.
The code sample above uses the ES6 modules import/export syntax.
If you use the CommonJS require() syntax, use the following import statement instead.
const fs = require('fs')
We used the fs.readdir() method to read the contents of the directory.
The method is asynchronous and returns an array of the names of the files in the directory.
The code sample above uses the older callback-style syntax.
In more modern code, you might want to use the async/await syntax.
import {readdir} from 'fs/promises'; // ๐๏ธ if you use the CommonJS require() syntax // const {readdir} = require('fs/promises') async function countFilesInDirectory(dirPath) { try { const files = await readdir(dirPath); console.log(files); return files.length; } catch (err) { console.log(err); } } const pathToDirectory = './my-folder'; countFilesInDirectory(pathToDirectory).then(count => { console.log(count); });
Notice that we imported the readdir()
method from fs/promises
this time.
The async
function awaits the Promise that the readdir()
method returns and
accesses the length
property on the array to get the number of files in the
directory.
You can also use the fs.readdirSync() method if you need to count the number of files in a directory synchronously.
import {readdirSync} from 'fs'; // ๐๏ธ if you use the CommonJS require() syntax // const {readdirSync} = require('fs') function countFilesInDirectory(dirPath) { try { const files = readdirSync(dirPath); console.log(files); return files.length; } catch (err) { console.log(err); } } const pathToDirectory = './my-folder'; const count = countFilesInDirectory(pathToDirectory); console.log(count);
This time we imported the readdirSync
method from the fs
module.
The method takes the path to the directory and synchronously reads its contents returning an array that contains the names of the files in the given directory.
The last step is to access the length
property on the returned array to get
the number of files.
If you need to count the number of files in a directory recursively, use the following code sample instead.
import {readdirSync, statSync} from 'fs'; import path from 'path'; // ๐๏ธ if you use the CommonJS require() syntax // const {readdirSync, statSync} = require('fs') // const path = require('path') function recursivelyCountFiles(dirPath, files) { const filesCurrentDir = readdirSync(dirPath); let allFiles = files || []; for (const fileName of filesCurrentDir) { const filePath = path.join(dirPath, fileName); if (statSync(filePath).isDirectory()) { allFiles = recursivelyCountFiles(filePath, allFiles); } else { allFiles.push(fileName); } } console.log(allFiles); return allFiles.length; } const pathToDirectory = './my-folder'; const count = recursivelyCountFiles(pathToDirectory); console.log(count);
readdirSync()
method to get the names of the files in the given
directory.recursivelyCountFiles()
function with the directory path and the array of files.allFiles
array.length
property of the array of files to get the
number of files in the directory recursively.You can learn more about the related topics by checking out the following tutorials: