Skip to content

Instantly share code, notes, and snippets.

@seanbehan
Forked from lovasoa/node-walk.es6
Created November 19, 2018 23:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanbehan/24bb12a1397487ecd26f214d0a64a136 to your computer and use it in GitHub Desktop.
Save seanbehan/24bb12a1397487ecd26f214d0a64a136 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