List only *.csv files

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

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.

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.

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

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 :slight_smile:

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

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 :slight_smile:

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.

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

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.

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

Alternatively this should also work

There is no real reason why the variable name needs to bear any relationship to the value.

char *ext = "mid";

could have become

char *ext = "csv"';

with no need to change the rest of the code, to list files with different extensions.

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

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

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

the strange thing is if I change the code from

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

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

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

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

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

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

}
  !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.

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

Onenate:
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.

        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.

				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?

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 :slight_smile:

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

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

okay I think I have figured out something but not too sure as I am running a w5100 card with the SD I have disabled the network part on start up

well I know now it is only an issue with creating it stop at

error: Write failed

should I change the line to something else?

#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[12];
        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 (plFile.open(file_list, O_CREAT|O_WRITE)){
    sd.errorHalt("Write failed");
  }
 
 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, 12);
				count++;
			}
		}
		file.close();
	
}
plFile.close();
Serial.println("count");
Serial.print(count); 
}

void loop() {}
SdFat sd;
SdFile file;
SdFile	plFile;		// Playlist file
	SdFile	lFile;		//list files
      	uint16_t count;		// count of files
        int  FNAME_SIZE;
        char  fname[12];
        char  file_list[] = "FILELIST.TXT";
        char *CSV_EXT = "CSV";

What's up
with the bizarre
indenting? Haven't
you been introduced to
Tools + Auto Format?

If not, you should learn how to use it.

with the bizarre indenting

it was from the original code from marco that I have changed to get working with what I need, I have cleaned up the code a bit more.

and I have tried to change the way it logs to a file separately they both work but together they don't instead of writing the list of files to a file can I write it to a buffer?

#include <SdFat.h>
#include <SdFile.h>

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

// file system object
SdFat sd;
SdFile file;
//SdFile	lFile;		//list files
uint16_t count;		// count of files
int FNAME_SIZE;
char fname[12];
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");

ofstream sdlog(file_list ,  ios::out | ios::app);

 

 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);			
  	         sdlog << (fname,12) << endl;
				count++;
			}
		}
		file.close();
	
}
Serial.println("count");
Serial.print(count); 







 if (!sdlog) sd.errorHalt("append failed");


sdlog.close();
}
 


void loop() {}

A few pretty simple questions:

  1. Do you actually have any csv files on the SD card?
  2. Can you read the SD card on a computer? Is it formatted? Is the write protection off?
  3. Have you tried any of the example programs that come with the libraries. There is usually at least one that displays the files on the SD card? If you run those, do they display files?
  4. Are you sure that there are no hardware conflicts (eg, Arduino pins for chip select)?

The odd formatting from my code seen when using the IDE comes from the fact I don't use the Arduino IDE and the editor uses real tabs. Just reformat as required.

and I have tried to change the way it logs to a file separately they both work but together they don't instead of writing the list of files to a file can I write it to a buffer?

I would suggest that you start by writing the file names to the serial console. Once you have mastered that you can write them anywhere.

if I run this code this is what I get with out creating the file so it is doing what it is meant to but just not writing it to a file.

program started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
3V_FILE.CSVStarted
Started
Started
TEST.CSVStarted
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
Started
10000101.CSVStarted
10000219.CSVStarted
10000308.CSVcount

#include <SdFat.h>
#include <SdFile.h>

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

// file system object
SdFat sd;
SdFile file;
//SdFile	lFile;		//list files
uint16_t count;		// count of files
char fname[12];
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");

//ofstream sdlog(file_list ,  ios::out | ios::app);

 

 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);
                			
  	         //sdlog << (fname,12) << endl;
				count++;
			}
		}
		file.close();
	
}
Serial.println("count");
Serial.print(count); 







 //if (!sdlog) sd.errorHalt("append failed");


//sdlog.close();
}