在Javascript中获取文件夹和文件列表的最佳方法

时间:2022-04-09 00:30:54

I'm using node-webkit, and am trying to have a user select a folder, and I'll return the directory structure of that folder and recursively get its children.

我正在使用node-webkit,我正在尝试让用户选择一个文件夹,然后我将返回该文件夹的目录结构并递归获取其子文件。

I've got this working fairly simply with this code (in an Angular Controller).

我用这个代码(在Angular Controller中)可以很简单地工作。

var fs = require('fs');

    $scope.explorer=[];
    $scope.openFile = function(){
            $scope.explorer = [tree_entry($scope.path)];    
            get_folder($scope.path, $scope.explorer[0].children);
    };

    function get_folder(path, tree){
        fs.readdir(path, function(err,files){
            if (err) return console.log(err);

            files.forEach( function (file,idx){
                tree.push(tree_entry(file));
                fs.lstat(path+'/'+file,function(err,stats){
                    if(err) return console.log(err);
                    if(stats.isDirectory()){
                        get_folder(path+'/'+file,tree[idx].children);
                    }
                });
            });
        });
        console.log($scope.explorer);

        return;
    }

    function tree_entry(entry){
        return { label : entry, children: []}
    }

Taking a moderate sized folder with 22 child folders and about 4 levels deep, it is taking a few minutes to get the entire directory structure.

使用具有22个子文件夹和大约4个级别的中等大小的文件夹,需要几分钟才能获得整个目录结构。

Is there something that I'm obviously doing wrong here? I can't believe it takes that long, seeing as I'm using the built in Node fs methods. Or is there a way to get the entire contents of a directory without touching each and every file?

有什么东西我在这里显然做错了吗?我不敢相信这需要很长时间,因为我正在使用内置的Node fs方法。或者有没有办法获取目录的全部内容而不触及每个文件?

I'm going to want to be able to use an Angular filter on the file names all the way down the tree, and possibly on the contents too, so delaying processing the entire tree isn't likely a solution that would work.

我希望能够在树的所有方向上对文件名使用Angular过滤器,也可能在内容上使用Angular过滤器,因此延迟处理整个树并不是一个可行的解决方案。

1 个解决方案

#1


37  

In my project I use this function for getting huge amount of files. It's pretty fast (put require("FS") out to make it even faster):

在我的项目中,我使用此函数来获取大量文件。它非常快(把require(“FS”)放出来让它更快):

var _getAllFilesFromFolder = function(dir) {

    var filesystem = require("fs");
    var results = [];

    filesystem.readdirSync(dir).forEach(function(file) {

        file = dir+'/'+file;
        var stat = filesystem.statSync(file);

        if (stat && stat.isDirectory()) {
            results = results.concat(_getAllFilesFromFolder(file))
        } else results.push(file);

    });

    return results;

};

usage is clear:

用法很明确:

_getAllFilesFromFolder(__dirname + "folder");

#1


37  

In my project I use this function for getting huge amount of files. It's pretty fast (put require("FS") out to make it even faster):

在我的项目中,我使用此函数来获取大量文件。它非常快(把require(“FS”)放出来让它更快):

var _getAllFilesFromFolder = function(dir) {

    var filesystem = require("fs");
    var results = [];

    filesystem.readdirSync(dir).forEach(function(file) {

        file = dir+'/'+file;
        var stat = filesystem.statSync(file);

        if (stat && stat.isDirectory()) {
            results = results.concat(_getAllFilesFromFolder(file))
        } else results.push(file);

    });

    return results;

};

usage is clear:

用法很明确:

_getAllFilesFromFolder(__dirname + "folder");