Created
September 3, 2019 08:40
-
-
Save wh1tew0lf/60340069d5c0adccdf89b7347b2c3ff3 to your computer and use it in GitHub Desktop.
taken from here https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
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
var fs = require('fs'); | |
var walk = function(dir, done) { | |
var results = []; | |
fs.readdir(dir, function(err, list) { | |
if (err) return done(err); | |
var i = 0; | |
(function next() { | |
var file = list[i++]; | |
if (!file) return done(null, results); | |
file = dir + '/' + file; | |
fs.stat(file, function(err, stat) { | |
if (stat && stat.isDirectory()) { | |
walk(file, function(err, res) { | |
results = results.concat(res); | |
next(); | |
}); | |
} else { | |
results.push(file); | |
next(); | |
} | |
}); | |
})(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment