ive been googling for two days but based on the limitations of SD.h you can only have one file open at a time so how would i complile a list of files that are on an sd card and store them in a file on the same sd card?
example:
user has random organic sd card with his or her favorite songs on it say 16000 songs, they insert the sd card and the arduino detects it and scans the sd card for mp3 or other music files and creates a file called mymp3s inside is a list of all the files that are on the sd card so that you can recurse it with arduino without having to use up a buunch of ram ...
tbillion:
ive been googling for two days but based on the limitations of SD.h you can only have one file open at a time so how would i complile a list of files that are on an sd card and store them in a file on the same sd card?
example:
user has random organic sd card with his or her favorite songs on it say 16000 songs, they insert the sd card and the arduino detects it and scans the sd card for mp3 or other music files and creates a file called mymp3s inside is a list of all the files that are on the sd card so that you can recurse it with arduino without having to use up a buunch of ram ...
You can open multiple files, you just need to watch your free ram.
MemoryFree is a simple library that can calculate the available RAM while a sketch is running.
found this code but its so old sdfat doesnt work like this anymore and if you have seen the documentation for sdfat its enormous, any help to update it? becasue i honestly have no clue what im doing.. js
#include <SdFat.h>
#include <SdFile.h>
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
SdFile file;
SdBaseFile lFile;
uint16_t count; Â Â // count of files
int const file_size =12;
char fname[file_size];
char file_list[] = "FILELIST.TXT";
char *CSV_EXT = "CSV";
#define SD_SELECT 4
// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void setup() {
 Serial.begin(9600);
    Â
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
Serial.println("program started");
if (lFile.open(file_list, O_CREAT|O_WRITE));
count = 0;
while (file.openNext(sd.vwd(), O_READ))
 {
   if (file.isFile())
  { file.getFilename(fname);
  if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
        Serial.println(fname);
        lFile.write(fname, file_size);   Â
      count++;
   }
  }
  file.close();
Â
}
lFile.close();
Serial.print("Count ");
Serial.print(count);
}
void loop() {}
oops, i guess we posted at the same time Chuck, i was comming to this conclusion about the two files open, when i found the above code. just when you use the arduino examples it says you can only have one open at a time. so i was going with it.
the code above hangs on -> file.getFilename(fname);
i wish arduino had something like www.planetsourcecode.com and i wish that when libraries get updated they would maintain some level of reverse compatability, its been an exhausting week, one problem to a next.
tbillion:
found this code but its so old sdfat doesnt work like this anymore and if you have seen the documentation for sdfat its enormous, any help to update it? becasue i honestly have no clue what im doing.. js
oops, i guess we posted at the same time Chuck, i was comming to this conclusion about the two files open, when i found the above code. just when you use the arduino examples it says you can only have one open at a time. so i was going with it.
the code above hangs on -> file.getFilename(fname);
i wish arduino had something like www.planetsourcecode.com and i wish that when libraries get updated they would maintain some level of reverse compatability, its been an exhausting week, one problem to a next.
Here is a simple routine to run thru the root directory and print out some info.
SdFile search;
if(!search.open("/",O_READ)){
Serial.println("open search Dir Failed");
}
else{
bool done=false;
while(!done){
SdFile f;
char name[65];
f.openNext(search,O_READ);
if(f.isOpen()){ // not at end
f.getName(name,64);
Serial.print(name);
Serial.print(" is a ");
if(f.isFile()) Serial.print("File");
else if(f.isDir()) Serial.print("SubDir");
Serial.print(' ');
Serial.println(f.fileSize,DEC);
f.close();
}
else done=true;
}
search.close();
}
thanks chuck, i a round robin sort of way you are ultra helpful
one thing is for sure is your example helped me understand what was up with that code...
correction to that line is -> file.getName(fname,12);
working code is anyone wants it is:
#include <SdFat.h>
//#include <SdFile.h>
// SD chip select pin
const uint8_t chipSelect = SS;
// file system object
SdFat sd;
FatFile file;
SdBaseFile lFile;
uint16_t count;Â Â // count of files
int const file_size =13;
char fname[file_size];
char file_list[] = "FILELIST.TXT";
char *CSV_EXT = "CSV";
#define SD_SELECT 4
// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void setup() {
 Serial.begin(9600);
    Â
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
Serial.println("program started");
if (lFile.open(file_list, O_CREAT|O_WRITE));
count = 0;
while (file.openNext(sd.vwd(), O_READ))
 {
   if (file.isFile())
  { file.getName(fname,12);
  if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
        Serial.println(fname);
        lFile.write(fname, file_size);  Â
      count++;
   }
  }
  file.close();
Â
}
lFile.close();
Serial.print("Count ");
Serial.print(count);
}
void loop() {}
i need to take you rexample and time it against the one i found see which one is faster.. yaya off to play... thanks Chuck!
few hours later now i am kinda getting burnt. the code works to make a file of filenames, what im stuck on now is recursion. i have had some varied results but none of them are correct. i can get it to add all the directories to a string, i can even get it to knock off the last directory it was in but i cant get it to stay where the files are, and the standard SD.h library that comes with IDE doesnt have vwd (slacking Arduino pple) and i cant just drop in sdfat this late in the game without a boat load of changes becasu SdFat is different. surely recursion isnt that hard an idea ive just been looking at it too long, maybe arduino is where i am messing up, im gonna go look up what a real computer would do, but in the mean time this code works as long as you set Rec to 0 when you call the function.
void SDFileWriteIndex(File dir, bool Rec, int numTabs) {
 String nameLen = "";
 int sizeLen = 0;
 double fileSizeN = 0;
 double N = 0;
 globalSDFIleCount = 0;
 double count = 0;
 char fname[11];
 char *CSV_EXT = "CSV";
 String countBase = "";
 root1 = SD.open("test.txt", FILE_WRITE);
 count = 0;
 while (true) {
  File entry = dir.openNextFile();
  if (! entry) {
   break;
  }
  //Serial.print(entry.name());
  if (! entry.isDirectory()) {             //Check if object is a Directory
   nameLen = entry.name();
   if (nameLen.endsWith(".BMP")) {
    //Serial.println(entry.name());
    //root1.println(nameLen);
   }
   count++;
  } else {
   if (Rec == 1) {                 //if recursive display mode is on iterate all files on device
    countBase = strlen(entry.name());
    CharS = countBase.toInt();             //convert the number to an integer
    Base = Base + "/" + entry.name();
    SDFileWriteIndex(entry, Rec, numTabs + 1);  //Call self for next item
    Serial.println(Base);
    //Base.remove(CharS + 1);
   }
  }
  entry.close();                   //Close Current item
  //delay(70);
 }
 root1.close();
}
still stuck, found some routines but they didnt work on the mega... am i really going to have to goto complicated sdfat? tiny fat doesnt even have a cwd routine. i mean how bleeping hard is it to look at the parent directories really?
rhetorical question pretty hard based on all the ways ive tried.
tbillion:
few hours later now i am kinda getting burnt. the code works to make a file of filenames, what im stuck on now is recursion. i have had some varied results but none of them are correct. i can get it to add all the directories to a string, i can even get it to knock off the last directory it was in but i cant get it to stay where the files are, and the standard SD.h library that comes with IDE doesnt have vwd (slacking Arduino pple) and i cant just drop in sdfat this late in the game without a boat load of changes becasu SdFat is different. surely recursion isnt that hard an idea ive just been looking at it too long, maybe arduino is where i am messing up, im gonna go look up what a real computer would do, but in the mean time this code works as long as you set Rec to 0 when you call the function.
How about the example in the SD reference: Listfiles
It uses recursive calls to list the content of an SD card to the Serial().
the code above hangs on -> file.getFilename(fname);
replaced with getname().
I use SDFat and it is no more complicated that SD.h. There are wrappers that make it a direct replacement for SD.h except for this one function becuase getFileName() cannot handle long file names.
i dont need liong file names or fat32, i mean its an arduino, not odroid or rpi... lol somewhere between SD.h and SDfat would be nice. wasted 3 days on this now and its just frustrating.
Maybe. Fat32 allows you to use bigger SD cards for storage. The only reason you may care is that low capacity cards (<500mB) are really hard to get these days.
Anyway, it looks like you have a working solution from the previous posts.
valid argument on the smaller cards thing. but i mean 64GB card in an arduino lol what would you use that for a 100 year study of climate change.. we are starting to sound like ron popiel "set it and forget it" lol and yeah the only thing that doesnt work is recursive directory information where i want to know what directory a filder is in which is udeful for when you compile the list of files you know where the file is and you can prepend the file with the complete file path.