Optimized directory listing with SDFat?

Dear SDFat experts,

I have to perform a large number of accesses to the SD card, and I think one of the bottlenecks in my code is where I read a whole directory and compile it into a variable.

My directory structure is very special (data tree), and I’m sure that a given directory only contains some subdirectories (or none) with names consisting of 1 digital character (0 to 9). So I want to fill a boolean array with that information for later processing.

I would like to be able to read the directory all at once (SDFat may be able to read the whole directory content in one shot somewhere on the SD card, doesn’t it?). This would certainly dramatically speed-up my code!

I’m really impressed by the speed of the ls(LS_R) function, and don’t understand why mine is so slow…
Any idea?

Here is my code:

  SdFile file;
  boolean tree_path[MAX_NUMBER_SIZE][10] = {false};  // Content of all levels of the tree leading to the current path
  char tmp_dir[SD_FILENAME_LENGTH] = {0};    // File names are DOS-type 8.3
  int pos = 0;
  int level = 0;

[...]
      level_empty = true;
      while (file.openNext(SD.vwd(), O_READ)) {
        file.getFilename(tmp_dir);
        file.close();
        pos = tmp_dir[0]-'0';
        if ((pos >= 0) && (pos <= 9)) {      // Check filename validity
          tree_path[level][pos] = true;    // Remember available paths at each level in our path
          level_empty = false;
        }
      }
[...]

I know it looks strange to open a file just to get its name... But that's what I found in the forums... :grin: