<SD.h> rename file on SD card or copy with new file name

Hi,

is it possible to rename files on a SD card?
At the moment I am filling up a file with data. at the end of the month, it should be named with the name of the month and the year (e.g. may21) and a new file should be created.
That's what I have:

void writesdcard()
{
  Serial.println("write to SD card");

  String fullstring = String(date) + "," + String(t) + "," + String(sumwatts);

  Serial.print("Full Data: ");
  Serial.println(fullstring);
  Serial.println("");


  File dataFile = SD.open("currentmonth.txt", FILE_WRITE);

  if (dataFile) {
    dataFile.println(fullstring);
    dataFile.close();

  }

  else {
    Serial.println("error opening currentmonth.txt");
  }
}

If It's not possible to rename the file and create a new one, maybe you can copy it with a new name and delete the old one? Or maybe you can read it somehow and write the content in a new created file?
I am using an esp8266.
The maximum size of the file will be only a few kB.

The SD library doesn't support renaming and it doesn't support having multiple files open at the same time so you can't just read one in and write it out (easily).

Why not just create the filename based on the current date from the start? In the current month, the filename will be "Sep21.txt" and on the first of next month, you start writing to "Oct21.txt". No renaming required.

ok, you are probably right.
so, I am not that good in programming. how would the code look like?
my month-name variable is
tm.tm_mday
maybe you could add it into my code easyly?

:-/

Yes I could, but you would learn nothing. Post your best effort and people will help.

Hint: You already have the filename kind of in your 'fullstring' variable.

Or maybe "Oct01.txt" ?
Or "20211001.txt" ? [2021-10-01]

you are absolutly right :slight_smile: thank you! I will try that maybe today or tomorrow and will tell you the result.

another good idea, maybe I give it a try, too

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.