-
-
Save darkcolonist/1f40370db9db5e625e9c to your computer and use it in GitHub Desktop.
List all files in a directory in Node.js recursively in a synchronous fashion
This file contains hidden or 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
| var walkSync = function(dir, recurse, filelist) { | |
| var fs = fs || require('fs'), | |
| files = fs.readdirSync(dir); | |
| filelist = filelist || []; | |
| recurse = recurse || false; | |
| files.forEach(function(file) { | |
| if (fs.statSync(dir + file).isDirectory()) { | |
| if(recurse){ | |
| filelist = walkSync(dir + file + '/', recurse, filelist); | |
| } | |
| } | |
| else { | |
| filelist.push(dir+file); | |
| } | |
| }); | |
| return filelist; | |
| }; | |
| console.log(walkSync("./", true)); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if recurse set to true