Skip to content
Advertisement

best way to get folder and file list in Javascript

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.

Advertisement

Answer

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");
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement