Go Down

Topic: List only *.csv files (Read 3287 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 for Arduino 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 for Arduino http://parola.codeplex.com

Onenate

thanks for you that but what your code is doing is exactly what I need it to do is create a list of files and then upload those files that end with .csv to a server

I have made the changes that you recommended but still get nothing it does not create the file.

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");
PgmPrintln("start of CSV List __________________>");
createPlaylistFile();


}

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[200] = "FILELIST.TXT" ;
        char *CSV_EXT = ".csv";
       

// open/create the playlist file
!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() {

}

PaulS

Quote
Alternatively this should also work

There is no real reason why the variable name needs to bear any relationship to the value.
Code: [Select]
char *ext = "mid";
could have become
Code: [Select]
char *ext = "csv"';
with no need to change the rest of the code, to list files with different extensions.

Onenate

for some strange reason it is still not creating the file so of course it will not log to it?

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");
PgmPrintln("start of CSV List __________________>");
createPlaylistFile();


}

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[200];
        char  file_list[] = "FILELIST.TXT";
       // #define *CSV_EXT "csv";
        char *CSV_EXT = ".CSV";
       
   !plFile.open("FILELIST.TXT", O_CREAT|O_WRITE);
//  {    sd.errorHalt("opening FILELIST.TXT for write failed");
// }

      Serial.println(file_list);

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){
plFile.write(fname, 100);
count++;
}
}
lFile.close();
}

// close the playlist file
plFile.close();
Serial.print(fname);
Serial.println("finished");
return(count);

}



void loop() {

}

Onenate

and if I change the char to  define CSV I get the following error

SdFatLs___list_csv.ino: In function 'uint16_t createPlaylistFile()':
SdFatLs___list_csv:101: error: expected `)' before ';' token
SdFatLs___list_csv:101: error: expected `)' before ';' token
SdFatLs___list_csv:101: error: expected primary-expression before ',' token
SdFatLs___list_csv:101: error: expected `)' before ';' token
SdFatLs___list_csv:101: error: expected `]' before ';' token
SdFatLs___list_csv:101: error: expected primary-expression before ')' token
SdFatLs___list_csv:101: error: expected `;' before ')' token

Onenate

the strange thing is if I change the code from

Code: [Select]
while (lFile.openNext(sd.vwd(), O_READ))

and  add a ! it seems to go in to an infinite loop

Code: [Select]
while (!lFile.openNext(sd.vwd(), O_READ))

but with out it, it does not seem to run the command at all.

Onenate

Done
start of CSV List __________________>
FILELIST.TXT
count 0
finished

Code: [Select]
  !plFile.open(file_list, O_CREAT|O_WRITE);
//  {    sd.errorHalt("opening FILELIST.TXT for write failed");
// }

      Serial.println(file_list);

count = 0;
  while (lFile.openNext(sd.vwd(), O_READ))
{
Serial.println("opened file");
  if (lFile.isFile())
{ lFile.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
plFile.write(fname, 100);
count++;
}
}
lFile.close();

}

// close the playlist file

plFile.close();
Serial.printlb(count);
Serial.print(fname);
Serial.println("finished");
return(count);

}

marco_c

Code: [Select]
  !plFile.open(file_list, O_CREAT|O_WRITE);

Not sure what the ! is doing there. I would also recommend that you do some error checking to see if the file is opened.

Quote
and add a ! it seems to go in to an infinite loop

which indicates that the call is failing.

The sdFat library uses openNext() exclusively. Many other libraries use a combination of openFirst() and then openNext(). You may need to check if that applies to the library you are using. If that is the case, then you need to change the file checking logic to be initialised outside the loop, something like:

file = OpenFirst()
while (file is open)
  do stuff
  file = openNext()
end while

Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

PeterH


the strange thing is if


If you think about what the code is doing, it's not strange at all. The only strange thing is why you think the command "isn't being run at all". In both cases, the command will always be run at least once if execution reaches that piece of code.
I only provide help via the forum - please do not contact me for private consultancy.

PaulS

Code: [Select]
        int  FNAME_SIZE;
        char  fname[200];

Since the SDFat and SD libraries only support fat16 format, which means 8.3 names, pissing away 200 bytes to hold a 12 character (including the trailing NULL) file names seems wasteful.

Code: [Select]
plFile.write(fname, 100);
You know (or should) how long the name is. Why are you stuffing 100 bytes of mostly garbage in the file?


Onenate

okay I have tired to simplify the code if I comment out the  //if (plFile.open(file_list, O_CREAT|O_WRITE)); the code works :)

but when I add it back in with the  if (plFile.open(file_list, O_CREAT|O_WRITE)); or even with out  plFile.open(file_list, O_CREAT|O_WRITE);

it completes but skips the openNext command, I am using IDE 1.0.3 and the lastest SDFAT library


Code: [Select]
#include <SdFat.h>
#include <SdFile.h>

// SD chip select pin
const uint8_t chipSelect = SS;

// file system object
SdFat sd;
SdFile file;
SdFile plFile; // Playlist file
SdFile lFile; //list files
      uint16_t count; // count of files
       int  FNAME_SIZE;
       char  fname[200];
       char  file_list[] = "FILELIST.TXT";
       char *CSV_EXT = "CSV";


// define a serial output stream
ArduinoOutStream cout(Serial);
//------------------------------------------------------------------------------
void setup() {
 Serial.begin(9600);
     
if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();
//if (plFile.open(file_list, O_CREAT|O_WRITE));

count = 0;
while (file.openNext(sd.vwd(), O_READ))
{
 Serial.println("Started");
    if (file.isFile())
{ file.getFilename(fname);
if (strcmp(CSV_EXT, &fname[strlen(fname)-strlen(CSV_EXT)]) == 0){
               Serial.print(fname);
        plFile.write(fname, 100);
count++;
}
}
file.close();

}
plFile.close();
Serial.println("count");
Serial.print(count);
}

void loop() {}

Go Up