Created
June 30, 2018 13:46
-
-
Save vithalreddy/b0388899942e4dea16df329ec3e5ec78 to your computer and use it in GitHub Desktop.
list of all files in a directory in Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//requiring path and fs modules | |
const path = require('path'); | |
const fs = require('fs'); | |
//joining path of directory | |
const directoryPath = path.join(__dirname, 'Documents'); | |
//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