Node.js fs.readdir 递归目录搜索 [英] Node.js fs.readdir recursive directory search

查看:32
本文介绍了Node.js fs.readdir 递归目录搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于使用 fs.readdir 进行异步目录搜索的任何想法?我意识到我们可以引入递归并使用下一个要读取的目录调用读取目录函数,但我有点担心它不是异步的...

Any ideas on an async directory search using fs.readdir? I realize that we could introduce recursion and call the read directory function with the next directory to read, but I'm a little worried about it not being async...

有什么想法吗?我看过 node-walk 这很棒,但不只给我文件在一个数组中,就像 readdir 一样.虽然

Any ideas? I've looked at node-walk which is great, but doesn't give me just the files in an array, like readdir does. Although

正在寻找类似...的输出

Looking for output like...

['file1.txt', 'file2.txt', 'dir/file3.txt']

推荐答案

基本上有两种方法可以实现这一点.在异步环境中,您会注意到有两种循环:串行和并行.串行循环在进入下一次迭代之前等待一次迭代完成——这保证了循环的每次迭代都按顺序完成.在并行循环中,所有的迭代都是同时开始的,一个可能在另一个之前完成,但是,它比串行循环快得多.因此,在这种情况下,最好使用并行循环,因为步行完成的顺序无关紧要,只要它完成并返回结果(除非您希望它们按顺序排列).

There are basically two ways of accomplishing this. In an async environment you'll notice that there are two kinds of loops: serial and parallel. A serial loop waits for one iteration to complete before it moves onto the next iteration - this guarantees that every iteration of the loop completes in order. In a parallel loop, all the iterations are started at the same time, and one may complete before another, however, it is much faster than a serial loop. So in this case, it's probably better to use a parallel loop because it doesn't matter what order the walk completes in, just as long as it completes and returns the results (unless you want them in order).

并行循环如下所示:

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 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 (stat && stat.isDirectory()) {
          walk(file, function(err, res) {
            results = results.concat(res);
            if (!--pending) done(null, results);
          });
        } else {
          results.push(file);
          if (!--pending) done(null, results);
        }
      });
    });
  });
};

串行循环如下所示:

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

改进的示例.

这篇关于Node.js fs.readdir 递归目录搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆