Skip to content

Instantly share code, notes, and snippets.

@jochemstoel
Forked from lovasoa/node-walk.es6
Created October 1, 2018 11:18
Show Gist options
  • Save jochemstoel/1bc4509608134873be47fd34f296ba96 to your computer and use it in GitHub Desktop.
Save jochemstoel/1bc4509608134873be47fd34f296ba96 to your computer and use it in GitHub Desktop.
Walk through a directory recursively in node.js.
var fs = require("fs"),
path = require("path");
function walk(dir, callback) {
fs.readdir(dir, function(err, files) {
if (err) throw err;
files.forEach(function(file) {
var filepath = path.join(dir, file);
fs.stat(filepath, function(err,stats) {
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile()) {
callback(filepath, stats);
}
});
});
});
}
if (exports) {
exports.walk = walk;
} else {
walk(".", manageFile);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment