This function will bring you back to the first file in the directory, used in conjunction with openNextFile()
.
file.rewindDirectory()
file
: an instance of the File class (returned by SD.open()).None.
#include <SD.h>
File root;
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
SD.begin(10);
root = SD.open("/");
printDirectory(root, 0);
Serial.println();
Serial.println("PRINT AGAIN");
Serial.println("-----------");
root.rewindDirectory(); // Return to the first file in the directory
printDirectory(root, 0);
Serial.println("Done!");
}
void loop() {
// Nothing happens after setup finishes
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// No more files
break;
}
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 {
// Files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
}
}