Skip to content

Instantly share code, notes, and snippets.

@farishan
Created February 24, 2023 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farishan/be14d06d94bcd0e4cb7d37dc194d4c27 to your computer and use it in GitHub Desktop.
Save farishan/be14d06d94bcd0e4cb7d37dc194d4c27 to your computer and use it in GitHub Desktop.
js script to get all filenames in a directory
//requiring path and fs modules
const path = require('path');
const fs = require('fs');
//joining path of directory
const directoryPath = path.join(__dirname, '');
//passsing directoryPath and callback function
fs.readdir(directoryPath, function (err, files) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
// Do whatever you want to do with the file
console.log(file);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment