0

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

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I couldn't stop laughing! It's been a long day... Thanks.
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.