在Javascript中获取文件夹和文件列表的最佳方式 [英] best way to get folder and file list in Javascript

查看:113
本文介绍了在Javascript中获取文件夹和文件列表的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这个代码(在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);
};
$ b函数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); $ b $ if(stats.isDirectory()){
get_folder(path +'/'+ file,tree [idx] .children);
}
});
});
});
console.log($ scope.explorer);

return;


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

以一个中等大小的文件夹与22个子文件夹和大约4个深度,需要几分钟来获得整个目录结构。



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

我希望能够在文件名下使用Angular过滤器,也可能在内容上使用Angular过滤器,所以延迟处理整棵树不太可能成为可行的解决方案。

解决方案

在我的项目中,我使用这个函数获取大量的文件。这很快(把 require(FS)出来让它更快):

  var _getAllFilesFromFolder = function(dir){

var filesystem = require(fs);
var results = [];
$ b fileystem.readdirSync(dir).forEach(function(file){

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

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

});

返回结果;

};

用法很明显:

  _getAllFilesFromFolder(__ dirname +folder); 


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.

I've got this working fairly simply with this code (in an 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.

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?

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.

解决方案

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):

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

这篇关于在Javascript中获取文件夹和文件列表的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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