Skip to content

Instantly share code, notes, and snippets.

@darkcolonist
Forked from kethinov/walksync.js
Last active September 10, 2015 09:03
Show Gist options
  • Select an option

  • Save darkcolonist/1f40370db9db5e625e9c to your computer and use it in GitHub Desktop.

Select an option

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
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));
@darkcolonist
Copy link
Author

if recurse set to true

[ './child1.js',
  './child2.js',
  './deep/child1.js',
  './deep/child2.js',
  './deep/deep/child1.js',
  './deep/deep/child2.js',
  './list.js',
  './parent.js' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment