Created
April 1, 2019 23:24
-
-
Save senthilmpro/c3e741a31a0625e3fe54f0887c430981 to your computer and use it in GitHub Desktop.
Node.js Read files from Folder
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
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