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

get all files in directory recursively nodejs

// You can use a globbing lib
const glob = require("glob");

var getDirectories = function (src, callback) {
  glob(src + '/**/*', callback);
};
getDirectories('test', function (err, res) {
  if (err) {
    console.log('Error', err);
  } else {
    console.log(res);
  }
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: JavaScript array to URL params 
Javascript :: loading react 
Javascript :: react-navigation 
Javascript :: javascript create object empty 
Javascript :: jquery get native element 
Javascript :: reverse a string 
Javascript :: negate regular expression 
Javascript :: how to add two times in javascript 
Javascript :: javascript functions 
Javascript :: updating an array of object in mongoose 
Javascript :: javascript sort 
Javascript :: how to go to last page after authentication 
Javascript :: json object 
Javascript :: javascript get object in object 
Javascript :: create cookie javascript react 
Javascript :: js if text contains lowercase 
Javascript :: destructuring in es6 
Javascript :: scroll up own 
Javascript :: mdn trim 
Javascript :: add column sequelize 
Javascript :: how to append item to an array in foreach javascript 
Javascript :: js how to find max value in an array 
Javascript :: como percorrer um objeto js 
Javascript :: install axios nodejs 
Javascript :: js promise api 
Javascript :: isupper 
Javascript :: nodejs request post 
Javascript :: convert json data to a html table 
Javascript :: make file from array save js 
Javascript :: how to remove last character from string in javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =