SHARE
TWEET

Untitled

a guest Jun 26th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3.  
  4. function filewalker(dir, done) {
  5.     let results = [];
  6.  
  7.     fs.readdir(dir, function(err, list) {
  8.         if (err) return done(err);
  9.  
  10.         var pending = list.length;
  11.  
  12.         if (!pending) return done(null, results);
  13.  
  14.         list.forEach(function(file) {
  15.             file = path.resolve(dir, file);
  16.  
  17.             fs.stat(file, function(err, stat) {
  18.                 // If directory, execute a recursive call
  19.                 if (stat && stat.isDirectory()) {
  20.                     // Add directory to array [comment if you need to remove the directories from the array]
  21.                     results.push(file);
  22.  
  23.                     filewalker(file, function(err, res) {
  24.                         results = results.concat(res);
  25.                         if (!--pending) done(null, results);
  26.                     });
  27.                 } else {
  28.                     results.push(file);
  29.  
  30.                     if (!--pending) done(null, results);
  31.                 }
  32.             });
  33.         });
  34.     });
  35. };
  36.  
  37. const allData = filewalker("./data", function(err, data) {
  38.     if (err) throw err;
  39.  
  40.     return data;
  41. });
  42.  
  43.  
  44.  
  45. console.log(allData);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top