Created
November 27, 2012 04:28
-
-
Save thesmart/4152363 to your computer and use it in GitHub Desktop.
readdirSyncRecursive - Recurse through a directory and populate an array with all the file paths beneath
This file contains hidden or 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
/** | |
* Recurse through a directory and populate an array with all the file paths beneath | |
* @param {string} path The path to start searching | |
* @param {array} allFiles Modified to contain all the file paths | |
*/ | |
function readdirSyncRecursive(path, allFiles) { | |
var stats = fs.statSync(path); | |
if (stats.isFile()) { | |
// base case | |
allFiles.push(path); | |
} else if (stats.isDirectory()) { | |
// induction step | |
fs.readdirSync(path).forEach(function(fileName) { | |
readdirSyncRecursive(path + "/" + fileName, allFiles); | |
}); | |
} | |
} | |
var allFiles = []; | |
readdirSyncRecursive("/path/to/search", allFiles); | |
console.info(allFiles); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment