node.js fs.readdir recursive directory search

前端 未结 30 1894
醉酒成梦
醉酒成梦 2020-11-22 15:55

Any ideas on an async directory search using fs.readdir? I realise that we could introduce recursion and call the read directory function with the next directory to read, bu

30条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-22 16:19

    Here's yet another implementation. None of the above solutions have any limiters, and so if your directory structure is large, they're all going to thrash and eventually run out of resources.

    var async = require('async');
    var fs = require('fs');
    var resolve = require('path').resolve;
    
    var scan = function(path, concurrency, callback) {
        var list = [];
    
        var walker = async.queue(function(path, callback) {
            fs.stat(path, function(err, stats) {
                if (err) {
                    return callback(err);
                } else {
                    if (stats.isDirectory()) {
                        fs.readdir(path, function(err, files) {
                            if (err) {
                                callback(err);
                            } else {
                                for (var i = 0; i < files.length; i++) {
                                    walker.push(resolve(path, files[i]));
                                }
                                callback();
                            }
                        });
                    } else {
                        list.push(path);
                        callback();
                    }
                }
            });
        }, concurrency);
    
        walker.push(path);
    
        walker.drain = function() {
            callback(list);
        }
    };
    

    Using a concurrency of 50 works pretty well, and is almost as fast as simpler implementations for small directory structures.

提交回复
热议问题