Only Read Some of the Folders on SD Card

Status
Not open for further replies.

lerxstrulz

Well-known member
I have a simple program that recursively iterates through the SD card, starting at root, and prints out all of the directories. However, it's only finding about half of the directories. I have tried reformatting the card and using all upper or lowercase letters. For some reason it does not see all of the folders.

Folders that are on the SD card:

data
effects
loops
profiles
sounds
sounds2
test1
test1e

After running the sketch, only the following directories are found:

effects
loops
profiles


Here is the sketch:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

AudioControlSGTL5000     audioShield;

/***
 * recursively list all files on SD card
 */
String dirSep = "";

int listDirectories(const char *path, char directories[][15])
{
   int index = 0;
   File dir = SD.open(path);
   dir.rewindDirectory();
   Serial.print("READ DIR: ");
   Serial.println(path);
   while(true && index < 99) {
     File entry = dir.openNextFile();
     if (! entry) {
      Serial.println("No More Files");
       // no more files
       if (dirSep != "") {
          dirSep = dirSep.substring(0, dirSep.length()-2);
       }
       break;
     }
     if (entry.isDirectory()) {
       Serial.print(" -> DIR -> ");
       Serial.println(entry.name());
       char *ret = strstr(entry.name(), "~");
       if (ret == NULL) {
         Serial.println(entry.name());
         strcpy(directories[index], entry.name());
         dirSep += "  ";
         index += listDirectories(entry.name(), directories);
         index++;
       }
     }  
     entry.close();
   }
   return index;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  // Always allocate memory for the audio shield!
  AudioMemory(16);
  // turn on audio shield
  audioShield.enable();
  // Check SD card
  SPI.setMOSI(7); 
  SPI.setSCK(14); 
  if (!(SD.begin(10))) {
     Serial.println("Unable to access the SD card");
  }
  delay(1000);
  char directories[99][15];
  int count = listFiles("/", directories);
}

void loop() {
  // put your main code here, to run repeatedly:
}

The output to serial monitor is:
Code:
READ DIR: /
 -> DIR -> EFFECTS
EFFECTS
READ DIR: EFFECTS
No More Files
 -> DIR -> TRASHE~1
 -> DIR -> SPOTLI~1
 -> DIR -> LOOPS
LOOPS
READ DIR: LOOPS
No More Files
 -> DIR -> PROFILES
PROFILES
READ DIR: PROFILES
No More Files
No More Files

I created all of the folders by putting the SD card in my Macbook Pro and using Finder. Anyone have any ideas why only a few of the folders are visible on the SD card? Thanks in advance!
 
I see long file names in subdirectory "EFFECTS" - only 8.3 names are supported - perhaps this is the trouble.

There is code at the bottom that filters those out.

But now that you mention it, I will delete those from the SD and see if that fixes anything.
 
Update: Tried a different SD card and same results. I used SDFormatter again to reformat the card, this time choosing the option to rewrite the logic when you click the "options" button, and that seems to have fixed the issue. Will post more if I find something else.
 
Status
Not open for further replies.
Back
Top