January 2020
Intermediate to advanced
530 pages
11h 11m
English
The walkDir() method is a recursive function. It takes a directory path, dir, as an input parameter and returns a list of all the files in the director. It's written as follows:
var walkDir = function(dir, done) { var results = []; fs.readdir(dir, function(err, fList) { if (err) return done(err); var maxLength = fList.length; if (!maxLength) return done(null, results); fList.forEach(function(file) { file = path.resolve(dir, file); fs.stat(file, function(err, stat) { if (stat && stat.isDirectory()) { walkDir(file, function(err, res) { results = results.concat(res); if (!--maxLength) done(null, results); }); } else { results.push(file); if (!--maxLength) done(null, results); } }); }); });Read now
Unlock full access