List all files in a directory using Node.js recursively in a synchronous fashion
The following program can be used to List all files in a directory using Node.js recursively in a synchronous fashion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var walkSync = function (dir, filelist) { var fs = fs || require('fs'), files = fs.readdirSync(dir); filelist = filelist || []; files.forEach(function (file) { if (fs.statSync(dir + file).isDirectory()) { filelist = walkSync(dir + file + '/', filelist); } else { console.log(dir + file) filelist.push(file); } }); return filelist; }; |
you can call the function walkSync by passing the folderPath which is to be iterated as an argument
1 |
var fileList = walkSync("src/"); |
it is important to add a / after the folder name.
Reference: https://gist.github.com/kethinov/6658166
Leave a Reply