How can I count the number of files in a directory using nodejs
with just plain JavaScript or packages? I want to do something like this:
How to count the number of files in a directory using Python
Or in bash script I'd do this:
getLength() { DIRLENGTH=1 until [ ! -d "DIR-$((DIRLENGTH+1))" ]; do DIRLENGTH=$((DIRLENGTH+1)) done }
Browse to the folder containing the files you want to count. Highlight one of the files in that folder and press the keyboard shortcut Ctrl + A to highlight all files and folders in that folder. In the Explorer status bar, you'll see how many files and folders are highlighted, as shown in the picture below.
The fs. readdirSync() method is used to synchronously read the contents of a given directory.
Use File Explorer Open the folder and select all the subfolders or files either manually or by pressing CTRL+A shortcut. If you choose manually, you can select and omit particular files. You can now see the total count near the left bottom of the window.
Using fs, I found retrieving the directory file count to be straightforward.
const fs = require('fs'); const dir = './directory'; fs.readdir(dir, (err, files) => { console.log(files.length); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With