Read file list from SD into array

Hi There,

Is there a way to create an array of all files on a SD card using Arduino?

This is a great way to list files and works: https://www.arduino.cc/en/Tutorial/LibraryExamples/Listfiles

but for some reason I can’t add them into the array below.

char *songList[] = { "saynogo.raw","livinproof.raw", "freedom.raw" }; 

Any help would be appreciated :wink:

The array has a fixed size…

Post your code

@JonasVorwerk, your topic has been moved to a more suitable location on the forum.

When posting code, please use code tags so we don't have to look at e.g. but it's properly rendered as [].

Thanks for your quick response!
Is there a solution so do this?

Sure
It starts by posting your code and what you have

Here is the code I'm working on, using a Teensy 3.2 and 4 with audio boards.


#include <SD.h>
#include <SPI.h>

File root;

//char *songList[] = { "audio/ordessa.raw", "audio/apache.raw", "audio/daisylad.raw","audio/amenloop.raw" };
char *songList[100] = {};

// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// Teensy audio board: pin 10
// Teensy 3.5 & 3.6 & 4.1 on-board: BUILTIN_SDCARD
// Wiz820+SD board: pin 4
// Teensy 2.0: pin 0
// Teensy++ 2.0: pin 20
const int chipSelect = 10;

int count = 0;

void setup(){
  //UNCOMMENT THESE TWO LINES FOR TEENSY AUDIO BOARD:
  SPI.setMOSI(7);  // Audio shield has MOSI on pin 7
  SPI.setSCK(14);  // Audio shield has SCK on pin 14  
  
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect.
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");
  printDirectory(root, 0);

  int songAmount = (sizeof(songList) / sizeof(songList[0]));
  Serial.print("songAmount: ");
  Serial.println(songAmount);
  //count = songAmount;

  Serial.print("count: ");
  Serial.println(count);

  Serial.println("done!");

}

void loop(){
  // nothing happens after setup finishes.
  delay(4000);
  Serial.println("list:");
  for(int i=0; i < 100; i++){
    Serial.println( songList[1] );
  }
  
}



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');
    }

    String file_name = entry.name();
    if ( isFileType(entry.name()) && file_name.indexOf('_') != 0 ) { // Here is the magic
      Serial.print(count); 
      Serial.print(" "); 
      char *tempname = entry.name();
      songList[count] = tempname; //add to array
      tempname = "";

      Serial.println(songList[count]); //show array item
      count++;
    }

    //Serial.print(entry.name());

    if (entry.isDirectory()) { // Dir's will print regardless, you may want to exclude these
      //Serial.print(entry.name());
      //Serial.println("/");
      //printDirectory(entry, numTabs+1);
    } else {
      // files have sizes, directories do not
      //Serial.print("\t\t");
      //Serial.println(entry.size(), DEC);
    }
    entry.close();
    
  }
}

bool isFileType(char* filename) {
  int8_t len = strlen(filename);
  bool result;
  if (  strstr(strlwr(filename + (len - 4)), ".mp3")
     || strstr(strlwr(filename + (len - 4)), ".aac")
     || strstr(strlwr(filename + (len - 4)), ".wma")
     || strstr(strlwr(filename + (len - 4)), ".wav")
     || strstr(strlwr(filename + (len - 4)), ".fla")
     || strstr(strlwr(filename + (len - 4)), ".mid")
     || strstr(strlwr(filename + (len - 4)), ".raw")
     // and anything else you want
    ) {
    result = true;
  } else {
    result = false;
  }
  return result;
}

you created space for 100 pointers