|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #1 on: February 18, 2013, 02:34:29 am » |
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
Offline
God Member
Karma: 15
Posts: 743
Big things come in large packages
|
 |
« Reply #2 on: February 18, 2013, 02:55:25 am » |
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); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #3 on: February 18, 2013, 05:48:53 am » |
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  /* * 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
Offline
God Member
Karma: 15
Posts: 743
Big things come in large packages
|
 |
« Reply #4 on: February 18, 2013, 06:02:54 am » |
#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  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);
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #5 on: February 18, 2013, 06:30:06 am » |
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() { }
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #6 on: February 18, 2013, 06:56:17 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #7 on: February 18, 2013, 04:42:50 pm » |
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() { }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #8 on: February 18, 2013, 04:46:20 pm » |
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
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #9 on: February 18, 2013, 04:56:32 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #10 on: February 18, 2013, 05:01:04 pm » |
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);
}
|
|
|
|
|
Logged
|
|
|
|
|
Sydney
Offline
God Member
Karma: 15
Posts: 743
Big things come in large packages
|
 |
« Reply #11 on: February 18, 2013, 05:42:46 pm » |
!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
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Tesla Member
Karma: 100
Posts: 6787
-
|
 |
« Reply #12 on: February 18, 2013, 05:46:52 pm » |
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
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 336
Posts: 36486
Seattle, WA USA
|
 |
« Reply #13 on: February 18, 2013, 05:52:18 pm » |
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?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Jr. Member
Karma: 0
Posts: 93
|
 |
« Reply #14 on: February 18, 2013, 05:52:38 pm » |
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 #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
|
|
|
|
|
|