Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs recursively read directory

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);
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: node js module export class 
Javascript :: js json groupby prop 
Javascript :: remove element from array in an immutable way 
Javascript :: javascript format seconds into minutes and second 
Javascript :: back button js 
Javascript :: how to use sweet alert in vue js 
Javascript :: get all classes of element jquery 
Javascript :: angular cli create component with module 
Javascript :: js encode to & 
Javascript :: javascript hover event 
Javascript :: jquery on dom change 
Javascript :: get child eleemtn by native element angular 
Javascript :: jquery if radio button is checked 
Javascript :: array left rotation javascript 
Javascript :: set data attribute with a string jquery 
Javascript :: character limit regex 
Javascript :: javascript set timeout 
Javascript :: sequelize get only one column 
Javascript :: get pods running on a node 
Javascript :: jquery unbind event 
Javascript :: add variable inside regex in javascript 
Javascript :: js get svg width 
Javascript :: javascript radio button onchange 
Javascript :: router navigatebyurl 
Javascript :: js code to take value from form and store it in a variable 
Javascript :: un hover in jquery 
Javascript :: text to 64base javascript 
Javascript :: how convert object to string and string to object in javascript 
Javascript :: mongoose find and update prop 
Javascript :: how to find the last object in an array 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =