node js read files recursively
var fs = require('fs');
var path = require('path');
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 = path.resolve(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();
}
});
})();
});
};
And to test it out on your home directory (WARNING: the results list will be huge if you have a lot of stuff in your home directory):
walk(process.env.HOME, function(err, results) {
if (err) throw err;
console.log(results);
});
Also in JavaScript:
- js check if array
- p5 js functions
- moment for react
- how to set up a success message show up if form is submitted in react hooks
- react js download file
- pop-under window before current page
- node check if file exists
- abstraction in javascript
- how to use flatlist keyextractor
- javascript keypress backspace not working
- sweet alert js
- comment dire le nombre de ligne html en cliquamt sur un boutton javascript
- javascript loop over classlist
- js toFixed
- check if localstorage key exists js
- redirect to page in javascript
- convert date to string javascript
- javascript max array
- javascript date difference in months
- moment.set
- add discord.js library
- angular add bootstrap
- javascript create object key from variable
- javascript convert date from mm/dd/yyyy to yyyymmdd