SdFat: counting number of files in directory?

How would I count the number of files in an SdFat directory and then access the nth file, to open or printName? Thanks.

This is possible but not very useful since the order of files in a FAT16/FAT32 directory is random.

Why do want to do this?

Thanks for prompt reply. I want to set a variable to the number of files in /LOGS directory, then display the filenames, 10 at a time, to my TFT screen. Down/Up arrows would page through the directory. Touching a file name would pass that filename to another function to display the contents of that file.

If the file order is random, why do I get the same order of listing each time I run openNext and printName, that is:

140302.CSV
140304.CSV
140314.CSV

By random order I mean files are created using the first free directory entry. This means you can't know where a new file will be created. Deleting a file causes the directory entry to be free.

You can use openNext() to count the files.

To open the nth file just skip files using openNext() until the nth file.

Yes, that would work, like this:

void ListLogs() {
int k = 0;
sd.chdir("/LOGS");
while (file.openNext(sd.vwd(), O_READ)) {
file.printName(&Serial);
k++;
Serial.println();
file.close();
}
sd.chdir("/");
Serial.print("There are ");
Serial.print(k);
Serial.println(" files in the logs directory");
}

But it would have to run through the directory once to count the files, and then repeatedly to get to the group of ten wanted. Is there a way to access the indicator that openNext uses and then open the nth file?

No.

Since used directory entries are randomly scattered through the directory file you can't seek to the nth used entry.

openNext() just sequentially reads the directory file.

OK, thanks.