Skip to content

Instantly share code, notes, and snippets.

@thesmart
Created November 27, 2012 04:28
Show Gist options
  • Save thesmart/4152363 to your computer and use it in GitHub Desktop.
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
/**
* 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