Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
114
rated 0 times [  117] [ 3]  / answers: 1 / hits: 143012  / 11 Years ago, sun, december 29, 2013, 12:00:00

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.


More From » node.js

 Answers
21

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

[#73506] Thursday, December 26, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
briannar

Total Points: 354
Total Questions: 103
Total Answers: 101

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;