Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created April 1, 2019 23:24
Show Gist options
  • Save senthilmpro/c3e741a31a0625e3fe54f0887c430981 to your computer and use it in GitHub Desktop.
Save senthilmpro/c3e741a31a0625e3fe54f0887c430981 to your computer and use it in GitHub Desktop.
Node.js Read files from Folder
const path = require('path');
const fs = require('fs');
function readFilesFromDir(dir) {
var filelist = [];
walkSync(dir, filelist);
return filelist;
}
function walkSync(dir, filelist) {
var files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function (file) {
if (fs.statSync(dir + '/' + file).isDirectory()) {
filelist = walkSync(dir + '/' + file, filelist);
}
else {
var fullPath = path.resolve(dir, file);
filelist.push(fullPath);
}
});
return filelist;
}
module.exports = {
readFilesFromDir : readFilesFromDir
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment