Arduino File.rewindDirectory()

Description

The File.rewindDirectory() function takes you back to the first file in the directory, used in conjunction with openNextFile().

Syntax

file.rewindDirectory()

Parameters

  • file: an instance of the File class.

Returns

  • None

Example Code

#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); 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 // Return to the first file in the directory dir.rewindDirectory(); 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); } } }

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.

※ OUR MESSAGES