1var fs = require('fs');
2var path = require('path');
3var walk = function(dir, done) {
4 var results = [];
5 fs.readdir(dir, function(err, list) {
6 if (err) return done(err);
7 var i = 0;
8 (function next() {
9 var file = list[i++];
10 if (!file) return done(null, results);
11 file = path.resolve(dir, file);
12 fs.stat(file, function(err, stat) {
13 if (stat && stat.isDirectory()) {
14 walk(file, function(err, res) {
15 results = results.concat(res);
16 next();
17 });
18 } else {
19 results.push(file);
20 next();
21 }
22 });
23 })();
24 });
25};
26
27
28And 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):
29
30walk(process.env.HOME, function(err, results) {
31 if (err) throw err;
32 console.log(results);
33});
1var fs = require('fs');
2var path = require('path');
3var walk = function(dir, done) {
4 var results = [];
5 fs.readdir(dir, function(err, list) {
6 if (err) return done(err);
7 var i = 0;
8 (function next() {
9 var file = list[i++];
10 if (!file) return done(null, results);
11 file = path.resolve(dir, file);
12 fs.stat(file, function(err, stat) {
13 if (stat && stat.isDirectory()) {
14 walk(file, function(err, res) {
15 results = results.concat(res);
16 next();
17 });
18 } else {
19 results.push(file);
20 next();
21 }
22 });
23 })();
24 });
25};
26
27And 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):
28
29walk(process.env.HOME, function(err, results) {
30 if (err) throw err;
31 console.log(results);
32});