Created
July 23, 2017 06:07
-
-
Save element6/398a055ade827a9e527742c5557b0a39 to your computer and use it in GitHub Desktop.
nodejs - list files
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
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