Skip to content

Instantly share code, notes, and snippets.

@subtubes-io
Created December 20, 2014 22:02
Show Gist options
  • Save subtubes-io/69d853b5856a7eb7b596 to your computer and use it in GitHub Desktop.
Save subtubes-io/69d853b5856a7eb7b596 to your computer and use it in GitHub Desktop.
Traverse a directory structure recursively with "fs" and node.js
var fs = require("fs");
function travese(path, seperator) {
var parts = path.split("/"),
last = parts[parts.length - 1],
files;
seperator = seperator || "";
if (fs.statSync(path).isDirectory()) {
//log the directory name
console.log(seperator + "[" + last + "]");
//get all the files in the directory
files = fs.readdirSync(path);
files.forEach(function (resourceName) {
//foreach file traverse and add an ellipsis to the separator
travese(path + "/" + resourceName, seperator + "...");
});
} else {
//log the separator and file name
console.log(seperator + " " + last);
}
}
travese("/Users/edgarmartinez/WebstormProjects/famou-list/_sample");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment