Skip to content

Instantly share code, notes, and snippets.

@nfeldman
Created June 15, 2012 01:39
Show Gist options
  • Save nfeldman/2934140 to your computer and use it in GitHub Desktop.
Save nfeldman/2934140 to your computer and use it in GitHub Desktop.
recursive readdir for node (via stackoverflow)
// http://stackoverflow.com/q/5827612/
function walk(dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var i = 0;
(function next() {
var file = list[i++];
if (!file) {
return done(null, results);
}
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (stat && stat.isDirectory()) {
walk(file, function (err, res) {
results = results.concat(res);
next();
});
} else {
results.push(file);
next();
}
});
}());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment