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.
I don't like adding new package into my project just to handle this simple task.
And also, I try my best to avoid RECURSIVE algorithm.... since, for most cases it is slower compared to non Recursive one.
So I made a function to get all the folder content (and its sub folder).... NON-Recursively
the only drawback of this function is that this is Synchronous function... You have been warned ;)
Why to invent the wheel?
There is a very popular NPM package, that let you do things like that easy.
Lear more:
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):usage is clear: