SD Card generate filename from date

ajreijn

Active member
Hi there. I'm stuck trying to figure out how to generate a different filename every day.

I came across this topic:


But I'm not sure how to apply that to my aim. I guess I need to work with char instead of strings...? Sorry I really lack some basic knowledge here and have been running in circles for ages now. Really hope someone could point me in the right direction.

Here is my code

Code:
#include <SD.h>
#include <I2C_RTC.h>

static PCF8563 RTC;
const int chipSelect = BUILTIN_SDCARD;

String filenameDaily;

void setup() {

  RTC.begin();
  RTC.setDate(16, 05, 24);  //SetDate(Day,Month,Year)
  RTC.setTime(12, 14, 00);  //SetTime(Hours,Minutes,Seconds)                           

  int rtcDay = (RTC.getDay());
  int rtcMonth = (RTC.getMonth());
  int rtcYear = (RTC.getYear());

  filenameDaily = String(rtcYear);
  filenameDaily += String(rtcMonth);
  filenameDaily += String(rtcDay);
  filenameDaily += ".csv";

}

void loop() {

  File dataFile = SD.open(filenameDaily, FILE_WRITE);

  Serial.println(filenameDaily);

}
 
one way

Code:
char FileName[25] = "YYYY-MM-DD_NN.csv";

sprintf(FileName, "%04d_%02d_%02d_%02d.csv",
          year(),
          month(),
          day(),   
          DataID);
          
          
// if you are using SDFat you can also add date/time attributes to the file

SDDataFile.timestamp(T_CREATE, year(), month(), day(), hour(), minute(), second());
SDDataFile.timestamp(T_WRITE, year(), month(), day(), hour(), minute(), second());
SDDataFile.timestamp(T_ACCESS, year(), month(), day(), hour(), minute(), second());
 
or this
Code:
char FileName[27] = "N_YYYY-MM-DD_NNN.csv";
 
 
 sprintf(FileName, "N_%04d_%02d_%02d_%03d.csv",
          ID,
          year(),
          month(),
          day(),   
          FileNumber);

    // populate ID
      if (someval == thisval) {
        FileName[0] = '0';
      } else if (someval == thatval) {
        FileName[0] = '1';
      } else {
        FileName[0] = '2';
      }

// file number to prevent overwrite of same file on same day
// code allows up to 1000 files (000-999)
      next = 0;
      while (SDCARD.exists(FileName)) {
        next++;

        FileName[13] = (int)((next / 100) % 10) + '0';
        FileName[14] = (int)((next / 10) % 10) + '0';
        FileName[15] = (int)(next % 10) + '0';
        if (next > 999) {
        // this is bad and not handled
          break;
        }
      }
 
Ok great! Thank you so much. I was considering to write (possibly multiple session per day) in the same file each day. It's more of a safety measure than that I need a very strict separation of data. But great to have the code at hand if I change my mind. I. I applied your code a bit, but couldn't quite get the code to compile yet though. What am I doing wrong here? :|

Code:
#include <SD.h>
#include <I2C_RTC.h>

static PCF8563 RTC;
const int chipSelect = BUILTIN_SDCARD;

char FileName[25] = "YYYY-MM-DD.csv";

void setup() {
 
  RTC.begin();
  RTC.setDate(16, 05, 24);  //SetDate(Day,Month,Year)
  RTC.setTime(12, 14, 00);  //SetTime(Hours,Minutes,Seconds)

  sprintf(FileName, "%04d_%02d_%02d.csv",
          (RTC.getYear(),
          (RTC.getMonth(),
          (RTC.getDay());                         

}

void loop() {

  File dataFile = SD.open(FileName, FILE_WRITE);

  Serial.println(FileName);

}
 
Last edited:
above code is a snippet, it does not show SD object creation or clock creation. Hence my object names are most likely different from yours

you will need to post code or indicated what the compile errors
 
this snippet may be better. Hopefully you can see how to use sprintf for build a string.


Code:
  sprintf(FileName, "%04d_%02d_%02d.csv",
          year(),
          month(),
          day());
 
I thought I did understand. Or not really (sprintf/C is somewhat of a mystery to me), but at least I thought my small adaptation of your code was correct. But apparently I'm doing something wrong. Sorry. Also understand you're not going to literally write my code.
 
sprintf is a mystery to me as well, but as far as I understand it sprintf(FileName, "%04d_%02d_%02d.csv", year(), month(), day()); will take the values of year(), month() and day() and create a filename something like 20240516.csv in the variable FileName.
 
Ok but I need to get the date information from my I2C clock while running my code in the field. So somehow 'year()' needs to point to - be replaced by - RTC.getYear() I think
 
I believe that is correct. Never used that library, but looks right.

You have SD.open stuff in a loop, so it's going to continually try to open before the file is closed.
 
Still getting this error though;

Compilation error: expected ')' before ';' token

So I'm guessing something is wrong with my syntax...?

The attached code is not the full code. Just what's relevant for this issue. But put a closing line in there now.

Code:
#include <SD.h>
#include <I2C_RTC.h>

static PCF8563 RTC;
const int chipSelect = BUILTIN_SDCARD;

char FileName[25] = "YYYY-MM-DD.csv";

void setup() {
 
  RTC.begin();
  RTC.setDate(16, 05, 24);  //SetDate(Day,Month,Year)
  RTC.setTime(12, 14, 00);  //SetTime(Hours,Minutes,Seconds)

  sprintf(FileName, "%04d_%02d_%02d.csv",
          (RTC.getYear(),
          (RTC.getMonth(),
          (RTC.getDay());                         

}

void loop() {

  File dataFile = SD.open(FileName, FILE_WRITE);

  dataFile.close();

  Serial.println(FileName);

}
 
Try change this

Code:
  sprintf(FileName, "%04d_%02d_%02d.csv",
          (RTC.getYear(),
          (RTC.getMonth(),
          (RTC.getDay());

to this

Code:
  sprintf(FileName, "%04d_%02d_%02d.csv",
          (RTC.getYear()),
          (RTC.getMonth()),
          (RTC.getDay())
  );
 
In the IDE hitting " CTRL + T " might have helped catch your eye

First is the uncorrected then Paul's posted after Ctrl+T - notice the shifting indenting Oddity ?
Code:
void setup() {

  RTC.begin();
  RTC.setDate(16, 05, 24);  //SetDate(Day,Month,Year)
  RTC.setTime(12, 14, 00);  //SetTime(Hours,Minutes,Seconds)

  sprintf(FileName, "%04d_%02d_%02d.csv",
          (RTC.getYear(),
           (RTC.getMonth(),
            (RTC.getDay());
}

void setup() {

  RTC.begin();
  RTC.setDate(16, 05, 24);  //SetDate(Day,Month,Year)
  RTC.setTime(12, 14, 00);  //SetTime(Hours,Minutes,Seconds)

  sprintf(FileName, "%04d_%02d_%02d.csv",
          (RTC.getYear()),
          (RTC.getMonth()),
          (RTC.getDay())
         );
}
 
Due to a bug in my code, I accidentally created a file with 8.8.3 length (filename.mid.mid) and was surprised that it worked.
I'm using the latest Teensy code for the SD library.
It looks like the 8.3 limitation is gone.
I'm seeing references on the web where SD fat has a 256 character limit... I'm conservatively guessing that for the whole path length, e.g:

midi/myreallylongmidifilename.mid
 
Back
Top