Pages: [1] 2   Go Down
Author Topic: Renaming files on the SD card  (Read 5986 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Netherlands
Offline Offline
God Member
*****
Karma: 7
Posts: 642
A naughty mind is a joy forever.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1605
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
 
//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:
if (SD.exists("now.csv"))
?
« Last Edit: September 23, 2011, 10:02:01 pm by feipoa » Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1605
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

To open or create a file for append in SdFat
Code:
  if (datafile.open("logfile.txt", O_WRITE | O_CREAT | O_APPEND)) {
    dataFile.println("Hello");
    dataFile.close();
  }
If you declared
Code:
SdFat sd;
use
Code:
  if (sd.exists("now.csv"))
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I do that I get,

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

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1605
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (card.exists("now.csv")) {
   now.remove("now.csv");
  }
« Last Edit: September 23, 2011, 10:43:58 pm by feipoa » Logged

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1605
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Read the html documentation and look at examples.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The documentation is horrific.

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

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1605
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

O.K. here is a sketch that checks for "myfile.txt".
Code:
#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() {}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Edison Member
*
Karma: 63
Posts: 1605
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1] 2   Go Up
Jump to: