Skip to content

Instantly share code, notes, and snippets.

@element6
Created July 23, 2017 06:07
Show Gist options
  • Save element6/398a055ade827a9e527742c5557b0a39 to your computer and use it in GitHub Desktop.
Save element6/398a055ade827a9e527742c5557b0a39 to your computer and use it in GitHub Desktop.
nodejs - list files
function _walkSync(dir, filter) {
const fs = require('fs');
const filelist = [];
const dirs = [dir];
while (dirs.length) {
const folder = dirs.pop();
fs.readdirSync(folder).forEach(file => {
const fullpath = folder + '/' + file;
if (fs.statSync(fullpath).isDirectory()) {
dirs.push(fullpath);
}
else if (!filter || file.endsWith(filter)) {
filelist.push(fullpath);
}
});
}
return filelist;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment