Go Down

Topic: Renaming files on the SD card (Read 6987 times) previous topic - next topic

feipoa

I've looked through the SD library for IDE 0022 but did not see a function which renames the file on the SD card.  Does anybody know how to rename SD card files or if there is another library out there which can?

I'm wanting to keep the last full day's data onto the SD card, always calling it yesturda.csv

So, when there is yesturda.csv with 24 hrs of data, then I create now.csv to start writing the next day's data.  When now.csv fills up 24 hours with of data, then I'll want to delete yesturda.csv and rename now.csv to yesturda.csv.  The next 24 hours starts again with a new now.csv.

As you can see, all I need is a file rename command.  Any ideas?  Thanks.

Simpson_Jr

Haven't looked into the standard library very much yet, but I do know the SDFat library renames files perfectly. I think it's a great piece of work.


http://code.google.com/p/sdfatlib/


feipoa

I've already written my cude using the standard SD library and was noping not to change all the syntax for SdFat.  Does anyone know how to add the rename feature from SdFAT into the standard SD library?

fat16lib

There is no practical way to add rename to SD.h.  SD.h is a wrapper for an old version of SdFat that does not support rename.

The newer SdFat with rename can't be used with the SD.h wrapper.

feipoa

#4
Sep 24, 2011, 04:57 am Last Edit: Sep 24, 2011, 05:02 am by feipoa Reason: 1
This is unfortunate.  Does anybody know when an updated IDE-standard SD library will exist with a rename feature?

Does anybody know the equivalent of this SD code using SdFat?

Code: [Select]
 
//if logfile.txt doesn't exist, SD.open creates a new file, if it exists, it appends data to it
 File dataFile = SD.open("logfile.txt", FILE_WRITE);  
 if (dataFile) {    // If the file is available, write to it
   dataFile.println("Hello");
   dataFile.close();
   }


How do I create this same functionality from SdFat?

Also, what is the equivalent of,

Code: [Select]
if (SD.exists("now.csv"))
?

fat16lib

To open or create a file for append in SdFat
Code: [Select]
  if (datafile.open("logfile.txt", O_WRITE | O_CREAT | O_APPEND)) {
    dataFile.println("Hello");
    dataFile.close();
  }

If you declared
Code: [Select]
SdFat sd;
use
Code: [Select]
  if (sd.exists("now.csv"))

feipoa

When I do that I get,

error: 'class Sd2Card' has no member named 'exists'

fat16lib

I said esists() is a member of the class SdFat, not Sd2Card.

feipoa

#8
Sep 24, 2011, 05:38 am Last Edit: Sep 24, 2011, 05:43 am by feipoa Reason: 1
I'm not sure I follow.  I am including SdFat.h already.  What else do I need to include to get exists() to function?


EDIT: The last features I need for the transition from SD to SdFat is the equivalent of this,

Code: [Select]
if (card.exists("now.csv")) {
   now.remove("now.csv");
  }

fat16lib

Read the html documentation and look at examples.

feipoa

The documentation is horrific.

Maybe somebody else here would like to exercise their skills of generocity?

fat16lib

O.K. here is a sketch that checks for "myfile.txt".
Code: [Select]
#include <SdFat.h>
SdFat sd;
const uint8_t CHIP_SELECT = 10;  // pin 10 is SD chip select
void setup() {
  Serial.begin(9600);
  if (!sd.init(SPI_HALF_SPEED, CHIP_SELECT)) sd.initErrorHalt();
  if (sd.exists("myfile.txt")) Serial.println("it exists");
}
void loop() {}

feipoa

Thank you for sharing this information.  I now see what you mean by the SdFat class.  I had "sd" as part of another class in my code.

While I can use the SdFat lib code to perform all my desired SD writing needs (in stand-alone sketches), it seems when I integrate it with all my other code the SD card isn't being written to.  It is probably not initializing at all.

I'm at a 25000 byte sketch size and am constantly trying to optimise code to save SRAM space.  Any new additions I make often set the Arduino into a resetting mode (implying I'm out of SRAM space), so I suspect that using SdFat over the SD class caused me to run out of SRAM.

Has there been any development towards introducing a renaming feature into the next IDE release for the SD class?

feipoa

Maybe there is another way to accomplish what I want with just the 0022 SD library. 

As I mentioned, I'm wanting to keep the last full day's worth data onto the SD card, always calling it yesturda.csv

So, when there is yesturda.csv with 24 hrs of data, then I create now.csv to start writing the next day's data.  When now.csv fills up 24 hours with of data, then I'll want to delete yesturda.csv and rename now.csv to yesturda.csv.  The next 24 hours starts again with a new now.csv.

Is there another way to always have yesturday's data on a static filename?  Thanks.

fat16lib

SD.h will not have rename anytime soon.  In Arduino 1.0 SD.h will be about two KB larger than the new SdFat and use more RAM.

You can do anything SD.h does with SdFat since SD.h is a simple wrapper for an old version of SdFat.  The SD.h wrapper mainly changes the default behavior for open(), and uses a different syntax for the API.

SD.h opens files so sync/flush is called after every write()/print().  This is very slow but insures data is written to the SD.  Also SD.h positions files opened for write at EOF.

Try opening your file with the flags in this sketch:
Code: [Select]

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

SdFile logfile;

// open like SD.h
const uint8_t OPEN_FLAGS = O_RDWR | O_CREAT | O_SYNC | O_AT_END;

const uint8_t SD_CHIP_SELECT = 10;

#define error(msg) sd.errorHalt_P(PSTR(msg))

void setup() {
  Serial.begin(9600);
  Serial.println("type any character to start");
  while (Serial.read() < 0);
 
  // initialize SdFat
  if (!sd.init(SPI_FULL_SPEED, SD_CHIP_SELECT)) sd.initErrorHalt();
 
  // use SD.h style flags
  if (!logfile.open("now.csv", OPEN_FLAGS)) error("open");
 
  // add a line to file
  logfile.println("line added to logfile");
 
  // skip sync and close to prove data is saved
  Serial.println("Done");
}
void loop() {}


Go Up