Created
December 20, 2014 22:02
-
-
Save subtubes-io/69d853b5856a7eb7b596 to your computer and use it in GitHub Desktop.
Traverse a directory structure recursively with "fs" and node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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