Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const path = require('path');
- function filewalker(dir, done) {
- let results = [];
- fs.readdir(dir, function(err, list) {
- if (err) return done(err);
- var pending = list.length;
- if (!pending) return done(null, results);
- list.forEach(function(file) {
- file = path.resolve(dir, file);
- fs.stat(file, function(err, stat) {
- // If directory, execute a recursive call
- if (stat && stat.isDirectory()) {
- // Add directory to array [comment if you need to remove the directories from the array]
- results.push(file);
- filewalker(file, function(err, res) {
- results = results.concat(res);
- if (!--pending) done(null, results);
- });
- } else {
- results.push(file);
- if (!--pending) done(null, results);
- }
- });
- });
- });
- };
- const allData = filewalker("./data", function(err, data) {
- if (err) throw err;
- return data;
- });
- console.log(allData);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement