Arduino File.openNextFile()
Description
The File.openNextFile() function opens the next file or folder in a directory.
Syntax
file.openNextFile()
Parameters
- file: an instance of the File class that is a directory
Returns
- A File object referring to the next file or folder in the path. if there is no next file or folder, this object will evaluate to false in a boolean context.
Example Code
/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/reference/library/arduino-file.opennextfile
*/
#include <SD.h>
#define PIN_SPI_CS 4
File root;
void setup() {
Serial.begin(9600);
if (!SD.begin(PIN_SPI_CS)) {
Serial.println("SD CARD FAILED, OR NOT PRESENT!");
while (1); // don't do anything more:
}
root = SD.open("/");
printDirectory(root, 0);
root.close();
}
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);
}
entry.close();
}
}
Tutorials
See Also
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.
Additionally, some links direct to products from our own brand, DIYables.