Last active
July 7, 2020 15:14
-
-
Save tspringborg/c23fc6c379da1256a432a0f18446baff to your computer and use it in GitHub Desktop.
synchronously recursively list directory files in nodejs with fs
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
const fs = require('fs'); | |
const walk = function (dir) { | |
let results = []; | |
const list = fs.readdirSync(dir); | |
list.forEach(function(file) { | |
file = dir + '/' + file; | |
const stat = fs.statSync(file); | |
if (stat && stat.isDirectory()) { | |
/* Recurse into a subdirectory */ | |
results = results.concat(walk(file)); | |
} else { | |
/* Is a file */ | |
results.push(file); | |
} | |
}); | |
return results; | |
}; | |
module.exports = walk; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment