A corrected example sketch for "openNextFile()"

The example sketch for "openNextFile()", located here has two flaws: (1) Once in a subdirectory, it never goes back to find more recent files in the root directory. (2) It then endlessly loops through sending "nomorefiles".

So here is my corrected sketch, which I hope you can use to replace the flawed one:

#include

File root;
void setup()
{
Serial.begin(9600);
pinMode(10, OUTPUT);
SD.begin(10);
root = SD.open("/");
printDirectory(root, 0);
}

void loop()
{
// nothing happens after setup finishes.
}

void printDirectory(File dir, int numTabs)
{
while(true)
{
File entry = dir.openNextFile();
if (! entry)
{
if (numTabs == 0)
Serial.println("** Done **");
return;
}
for (uint8_t i=0; i<numTabs; i++)
Serial.print('\t');
Serial.print(entry.name());
if (entry.isDirectory())
{
Serial.println("/");
printDirectory(entry, numTabs+1);
}
else
{
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
}
}

So here is my corrected sketch

Does that even compile for you? For me:

sketch_jun20a.ino:1:9: error: #include expects "FILENAME" or
sketch_jun20a:6: error: variable or field 'printDirectory' declared void
sketch_jun20a:6: error: 'File' was not declared in this scope
sketch_jun20a:6: error: expected primary-expression before 'int'
sketch_jun20a:3: error: 'File' does not name a type
sketch_jun20a.ino: In function 'void setup()':
sketch_jun20a:8: error: 'SD' was not declared in this scope
sketch_jun20a:9: error: 'root' was not declared in this scope
sketch_jun20a:10: error: 'printDirectory' was not declared in this scope
sketch_jun20a.ino: At global scope:
sketch_jun20a:18: error: variable or field 'printDirectory' declared void
sketch_jun20a:18: error: 'File' was not declared in this scope
sketch_jun20a:18: error: expected primary-expression before 'int'

@CosmickGold:
Could you please post your corrected example, preferably after a pass throught the IDE's auto-format tool?