find files by extension, *.html under a folder in nodejs

后端 未结 14 798
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 23:57

I\'d like to find all *.html files in src folder and all its sub folders using nodejs. What is the best way to do it?

var folder = \'/project1/src\';
var ex         


        
14条回答
  •  無奈伤痛
    2020-12-08 00:22

    I just noticed, you are using sync fs methods, that might block you application, here is a promise-based async way using async and q, you can execute it with START=/myfolder FILTER=".jpg" node myfile.js, assuming you put the following code in a file called myfile.js:

    Q = require("q")
    async = require("async")
    path = require("path")
    fs = require("fs")
    
    function findFiles(startPath, filter, files){
        var deferred;
        deferred = Q.defer(); //main deferred
    
        //read directory
        Q.nfcall(fs.readdir, startPath).then(function(list) {
            var ideferred = Q.defer(); //inner deferred for resolve of async each
            //async crawling through dir
            async.each(list, function(item, done) {
    
                //stat current item in dirlist
                return Q.nfcall(fs.stat, path.join(startPath, item))
                    .then(function(stat) {
                        //check if item is a directory
                        if (stat.isDirectory()) {
                            //recursive!! find files in subdirectory
                            return findFiles(path.join(startPath, item), filter, files)
                                .catch(function(error){
                                    console.log("could not read path: " + error.toString());
                                })
                                .finally(function() {
                                    //resolve async job after promise of subprocess of finding files has been resolved
                                    return done();
                                 });
                        //check if item is a file, that matches the filter and add it to files array
                        } else if (item.indexOf(filter) >= 0) {
                            files.push(path.join(startPath, item));
                            return done();
                        //file is no directory and does not match the filefilter -> don't do anything
                        } else {
                            return done();
                        }
                    })
                    .catch(function(error){
                        ideferred.reject("Could not stat: " + error.toString());
                    });
            }, function() {
                return ideferred.resolve(); //async each has finished, so resolve inner deferred
            });
            return ideferred.promise;
        }).then(function() {
            //here you could do anything with the files of this recursion step (otherwise you would only need ONE deferred)
            return deferred.resolve(files); //resolve main deferred
        }).catch(function(error) {
            deferred.reject("Could not read dir: " + error.toString());
            return
        });
        return deferred.promise;
    }
    
    
    findFiles(process.env.START, process.env.FILTER, [])
        .then(function(files){
            console.log(files);
        })
        .catch(function(error){
            console.log("Problem finding files: " + error);
    })
    

提交回复
热议问题