7

I'm creating a Chrome application. I must read the files of a directory and I am using the DirectoryEntry API and DirectoryReader API.

My problem is that the maximum files read using DirectoryReader#readEntries is 100, the first 100 (alphabetical order)

var reader = currentDir.createReader();
var read = reader.readEntries.bind(reader, function(files) {
    for ( var i = 0; i < files.length; i++){
         if (files[i].name == nameSearches){
                callback(files[i]);
            }

        }
    })
    callback(undefined)
}
read();

The value of files.length is 100 and there are more files in the directory

I'm not sure if this limitation is about Google Chrome, Google Chrome Applications, Javascript... and if this limitation can be overcomed

With the solution marked the result code is this:

var reader = currentDir.createReader();
var read = reader.readEntries.bind(reader, function(files) {
    if (files.lenght == 0) {
        callback(undefined);
    }
    for ( var i = 0; i < files.length; i++){
        if (files[i].name == nameSearches){
                callback(files[i]);
            }

        }
    })
    read();

}
read();
Xan
  • 74,770
  • 16
  • 179
  • 206
fhuertas
  • 4,764
  • 2
  • 17
  • 28
  • You may also have a look on [bro-fs](https://github.com/vitalets/bro-fs) package that solves this issue and also provides handy promise-based interface to files API. – vitalets Sep 19 '19 at 16:23

1 Answers1

19

Read the docs you linked!

The only method for this interface, readEntries() is for listing all the files and folders in a directory. To list all the entries, you need to do the following:

  1. Call directoryEntry.createReader() to create a new DirectoryReader.
  2. Call readEntries().
  3. Continue calling readEntries() until an empty array is returned. You have to do this because the API might not return all entries in a single call.
Xan
  • 74,770
  • 16
  • 179
  • 206
  • A lot of thanks. I was just thinking about that solution after writing the post. And obviously works. And the example explains it (https://developer.mozilla.org/en-US/docs/Web/API/DirectoryReader). I must improve search for solutions... – fhuertas May 23 '14 at 07:48
  • 1
    WTF the readEntries() reads just a part of the entries. Bad design, the function should explicitly accept a parameter of maximum number of entries returned per call (with NO default value). How do you "continue calling the function" if the function is asyncronous? – Gianluca Ghettini Dec 07 '17 at 09:56
  • Gianluca Ghettini : You create a recursive function that calls readEntries again if the result of read entries isn't an empty array. So if you have 325 items, it will be called five times, returning 100, 100, 100, 25 then 0 items. Once it returns zero, you don't call it again. – Pablo Barría Urenda Aug 15 '18 at 15:20
  • Oh boy this sucks bigtime, because it looks like it is working and only when you try it with a lot of files it's incomplete. I'm with Gianluca here, there should be a required parameter of some kind to acknowledge this behaviour. – fiffy Jun 05 '20 at 08:35