Go Down

Topic: List only *.csv files (Read 3 times) previous topic - next topic

Onenate

hey guys are am trying to write some code that will only list the *.CSV files in the root directory

I have found this but have no idea how to use it

uint8_t SdBaseFile::type  (  ) const 

http://mosquino.googlecode.com/hg/libraries/sdfatlib-mosquino/html/class_sd_base_file.html#a1555e6866abc9c867c5fff189a3a4317

any help would be great

PaulS

That function determine whether the instance is a directory or a file, not what the extension of a file is.

Seeing your code would be useful.

marco_c

Code from a program that build up a list of file name ending with MIDI_EXT (.MID). You should be able to adapt it for your use somehow, or just find out how you do what you need. I used the sdFat library, so the function calls may be different, and it created a file on the SD card with all the file names that I could then use to display in a menu.

Code: [Select]
uint16_t createPlaylistFile(void)
// create a playlist file on the SD card with the names of the files.
// This will then be used in the menu.
{
SdFile plFile; // Playlist file
SdFile mFile; // MIDI file
uint16_t count; // count of files
char fname[FNAME_SIZE];

// open/create the playlist file
if (!plFile.open(PLAYLIST_FILE, O_CREAT|O_WRITE))
LCDErrMessage("PL create fail", true);

count = 0;
while (mFile.openNext(SD.vwd(), O_READ))
{
if (mFile.isFile())
{
mFile.getFilename(fname);
if (strcmp(MIDI_EXT, &fname[strlen(fname)-strlen(MIDI_EXT)]) == 0)
// only include files with MIDI extenstion
{
plFile.write(fname, FNAME_SIZE);
count++;
}
}
mFile.close();
}

// close the playlist file
plFile.close();

return(count);
}
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Onenate

thanks Marco_c  that has give me something to start with i have added it to the basic sdfatls code I have tried to fixup some of the references you had in their just checking how did you declare the midi_ext I thought it maybe like 

char           CSV_EXT[] =  ".csv";

but I think I may be wrong some help to clean up the mess I created would be much appreciated :)



Code: [Select]


/*
* This sketch will list all files in the root directory and
* then do a recursive list of all directories on the SD card.
*
*/

#include <SdFat.h>
#include <SdFatUtil.h>

Sd2Card card;
SdVolume volume;
SdFile root;
SdFat sd;


// store error strings in flash to save RAM
#define error(s) error_P(PSTR(s))

void error_P(const char* str) {
  PgmPrint("error: ");
  SerialPrintln_P(str);
  if (card.errorCode()) {
    PgmPrint("SD error: ");
    Serial.print(card.errorCode(), HEX);
    Serial.print(',');
    Serial.println(card.errorData(), HEX);
  }
  while(1);
}

void setup() {
  Serial.begin(9600);
  PgmPrintln("Type any character to start");
// Serial.print("SS_PIN "); Serial.println(SS_PIN, DEC);
  while (!Serial.available());

  PgmPrint("Free RAM: ");
  Serial.println(FreeRam());

  // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with
  // breadboards.  use SPI_FULL_SPEED for better performance.
  // if (!card.init(SPI_HALF_SPEED))

  pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
  digitalWrite(10, HIGH); // but turn off the W5100 chip!
  if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!");

  // initialize a FAT volume
  if (!volume.init(&card)) error("vol.init failed!");

  PgmPrint("Volume is FAT");
  Serial.println(volume.fatType(),DEC);
  Serial.println();

  if (!root.openRoot(&volume)) error("openRoot failed");

  // list file in root with date and size
  PgmPrintln("Files found in root:");
  root.ls(LS_DATE | LS_SIZE);
  Serial.println();

  // Recursive list of all directories
  PgmPrintln("Files found in all dirs:");
  root.ls(LS_R);


  Serial.println();
  PgmPrintln("Done");
}

uint16_t createPlaylistFile(void)
// create a playlist file on the SD card with the names of the files.
// This will then be used in the menu.
{
SdFile plFile; // Playlist file
SdFile lFile; //list files
            uint16_t count; // count of files
       int          FNAME_SIZE;
        char fname[128];
        char            file_list[100];
        char           CSV_EXT[] =  ".csv";
       

// open/create the playlist file
if (!plFile.open(file_list, O_CREAT|O_WRITE))
//LCDErrMessage("PL create fail", true);

count = 0;
while (lFile.openNext(sd.vwd(), O_READ))
{
if (lFile.isFile())
{
lFile.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0)
// only include files with MIDI extenstion
{
plFile.write(fname, 100  );
count++;
}
}
lFile.close();
}

// close the playlist file
plFile.close();

return(count);
}



void loop() {

createPlaylistFile(); }


marco_c

Code: [Select]
#define MIDI_EXT ".MID"

What you have is more efficient if the compiler can't recognise multiple uses as the same thing.

Alternatively this should also work

Code: [Select]
char *csv_ext = ".csv";

You do understand that my code creates a file on the SD card with the list of names in it? This is probably not what you want, exactly.

You also should call the function once from setup() and not every time through the loop. The list of files will not change every a few milliseconds :)

Think about what you have done with this one - what is executed if the 'if' is true? if you are not handling an error then get rid of the if statement or call your error function instead.
Code: [Select]
if (!plFile.open(file_list, O_CREAT|O_WRITE))
//LCDErrMessage("PL create fail", true);


Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Go Up