Pages: [1] 2 3 4   Go Down
Author Topic: List only *.csv files  (Read 2256 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

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

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley



Code:

/*
 * 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(); }
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#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:
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 smiley

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:
if (!plFile.open(file_list, O_CREAT|O_WRITE))
//LCDErrMessage("PL create fail", true);

Logged

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

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 * 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() {
 
 }
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
/*
 * 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() {
 
 }
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

the strange thing is if I change the code from

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

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

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

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

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
  !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);

}
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  !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

Logged

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

UK
Offline Offline
Shannon Member
****
Karma: 183
Posts: 11130
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
        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:
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?

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

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:
#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() {}
Logged

Pages: [1] 2 3 4   Go Up
Jump to: