I'm trying to create a way to make a recursive list of the long name of files from an SD card with an index so that I call the file I'd like. Right now, I'm having trouble getting the files to list (!CFile.open() is always returning true regardless of the number of files on the card)

#include "SdFat.h"
#include "FreeStack.h"

// SD card chip select pin.
const uint8_t SD_CS_PIN = 10;

SdFat sd;
SdFile file;
SdFile dirFile;

// Number of files found.
uint16_t numberOfFiles = 0;

// Position of file's directory entry.
uint16_t dirIndex[300];
//**Is there a way I make an array without allocating the size here? 
void setup() 
  while (!Serial) {}

  // Initialize at the highest speed supported by the board that is
  // not over 50 MHz. Try a lower speed if SPI errors occur.
  if (!sd.begin(SD_CS_PIN, SD_SCK_MHZ(50))) 
  if (dirFile.open("/", O_READ)) 
  Serial.print("Reading: ");
  printDirectory(&dirFile, 0);
void printDirectory (SdFile* CFile, int numTabs)
  while (file.openNext(CFile, O_READ)) 
    if (file.isHidden()||file.)
      //file hidden, skip
      for (uint8_t i = 0; i < numTabs; i++) 
      if (file.isSubDir())
        printDirectory(&file, numTabs++);
        // Save dirIndex of file in directory.
        dirIndex[numberOfFiles] = file.dirIndex();
        // Print the file number and name.
        Serial.write(' ');

If someone could help me catch what I've got wrong, I'd like to get a second opinion.

Edit: Updated Code

Edit 2: I'm now getting the first level, but the second level is not printing I think I may be doing something wrong with pointers?


dirFile.open("/", ...) opens the root directory which always exist, even if there are no files.

You can check if there are files by checking openNext. If the first call returns false, there are no files or folders.

Some unrelated remarks:

  • Name variables you use, with more than 1 character (so e.g. numberOfFiles instead of n)
  • Do not increase a variable inside a Serial.print statement .. if you remove the 'test/debug' code, your sketch behaves differently.


I now see your question about the array. 300 is probably what you assume is the maximum number of files. Note that if 300 is passed, the sketch will crash.

What you can do to prevent this:

  • Easy solution: stop storing files after 300. In this case, make 300 a constant.
  • Solid solution: do not use a predetermined length array. However, this means some disadvantages:
    • You have to create a list structure and dynamically change the code.
    • This is slower (probably not an issue since SD card communication is much slower)
    • It can result in memory gaps, resulting in hard to solve memory problems later if the code is extended and more memory is needed.

I suggest you add some code, to stop storing at 300.

  • Ok, changing if (!CFile.open("/", O_READ)) to if (!CFile.openNext(&dirFile, O_READ)) has no effect. There seems to be something wrong with the printDirectory function – ATE-ENGE Jun 6 '17 at 15:17
  • Hey, I think I updated the code to reflect what you were saying. Can you let me know if you see something else I'm missing? – ATE-ENGE Jun 6 '17 at 15:52
  • 1
    It's a bit hard to easily see if it's complete ... the best way is to create several empty/non empty sub directories and iterate through them (e.g. running the sketch) and check against the output on Serial. – Michel Keijzers Jun 6 '17 at 16:28
  • 1
    At least the comment "// THIS is returing true regardless of if there are files on the card should be removed now :-) – Michel Keijzers Jun 6 '17 at 16:29
  • Ok, I've updated the code to something that is working better. I think there's still something that needs to be fixed though. XD – ATE-ENGE Jun 6 '17 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.