Abdul Sadik Yalcin
Abdul Sadik Yalcin

Reputation: 1822

Retrieve all directories with file system api

Is there a way to count all directories within a folder with file system api without being stuck in an infinite loop!?

Can't figure out why or where the leak is but I eventually had to quit my app after it got to 50K - it never looked like reaching an end point. I am on OS X so is it counting all the hidden directories and files such as DS Store?

//path: my folder
app.workspace.getDirectory(path, {}, function(directory){
    var reader = directory.createReader();
    if(directory){
        reader.readEntries(function(entries){
            for(var i = 0; entries.length; i++) {
                if(entries.isDirectory){
                    console.log('Directory: ', entries[i]);
                }
                else {
                    //don't need to know anything else...
                }
            }
        });
    }
}, error);

Upvotes: 0

Views: 772

Answers (2)

vitalets
vitalets

Reputation: 4983

You can try bro-fs that can read directories recursively:

fs.readdir('dir', {deep: true})
  .then(tree => console.log(tree))

Upvotes: 0

Marcelo
Marcelo

Reputation: 4435

Your for loop is never checking i against entries.length when it iterates.

Modify for(var i = 0; entries.length; i++)
to for(var i = 0; i < entries.length; i++)

Additionally, I assume if(entries.isDirectory)
should be if(entries[i].isDirectory).

Upvotes: 3

Related Questions