Name your Text(.txt) file with the actual date on the SD card for logging

i notice that many people looks for this so here is what i wrote to write txt file...

first you may have a RTC clock or you might be using the librearies...
so it is important to have this libraries working

#include <DS1307RTC.h>
#include <Time.h>

make sure your RTC comands work well, the function below should print the date on the serial port.

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
  
}

Now the function to convert INT to STRING to name your file...

void file_name()//function to name the text files each day...
{
  char file_today_name[30];//declare this function as a global..i just put it here for demostration
  char s_day[9],s_month[9],s_year[9];// Variables for storing the conversion from int to string
  const char zero[]="0";// Zero to add when ever the month is 9.. to be 09, so you always have the same string lenght

  
 itoa(day(),s_day,10);// convert day to string
 itoa(month(),s_month,10);  //convert month to string
 itoa(year(),s_year,10);  //convert year to string
 
            for(int i=0;i<30;i++)// wipe the file name to and empty string
           {                     // if not it will keep appending the string in the copy functions
             file_today_name[i]='\0';
                
            }
       

 if(month()<10);strcat(file_today_name,zero);// add trailing zeros for month
 strcat(file_today_name,s_month);
 if(day()<10);strcat(file_today_name,zero); // add trailing zeros for day
 strcat(file_today_name,s_day); // concatenate strings
 strcat(file_today_name,s_year);//concatenate strings
 strcat(file_today_name,".txt");// file name stored in file_today with .txt
 
 //Serial.print(file_today_name);
}

now before writing the SD card make sure you run the function

file_name();

 if (!sd.init(SPI_HALF_SPEED, 4)) sd.initErrorHalt();
   // open the file for write at end like the Native SD library 
  
  if (!myFile2.open(file_today_name, O_RDWR | O_CREAT | O_AT_END)) 
                 {
stuff.........
}

this should take care of your needs to name files with the current date...

I'm not sure why you're setting file_today_name[30] when the maximum length of a filename is 12 characters (8.3). Seems file_today_name[13] would be adequate, and save a few bytes of RAM. Am I missing something?

fyi - there is DateString.cpp in the Time library..

Here's the code I've used to do something similar:

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <SdFat.h>

char logFile[13];
void setup() {
  time_t t = now();
  int yr = year(t) - 2000;
  sprintf(logFile,"%.2d%.2d%.2d00.csv",
  yr, month(t), day(t));
    if(myFile.open(logFile,O_RDWR | O_CREAT | O_AT_END)) {
stuff...
  }
  else {
  error
  }
}

Of course, if you wanted a four-digit year, you'd dispense with the int yr declaration and just use year(t) in the sprintf statement.

thanks guys... more examples!!!!

i agree with the file_today_name[30] xd :slight_smile:

It was super complicated for me at first but I understand it now. I'll upload just the DS3231 Code with the SD card functionality

#include <Wire.h>
#include <DS3231.h>
#include <SPI.h>
#include <SD.h>

DS3231 clock;
RTCDateTime dt;
File myFile;
String fileName = "000000.txt";

void setup()
{
Serial.begin(9600);

// Initialize DS3231
Serial.println("Initialize DS3231");;
clock.begin();

// Set sketch compiling time
clock.setDateTime(DATE, TIME);

// Set from UNIX timestamp
// clock.setDateTime(1397408400);

// Manual (YYYY, MM, DD, HH, II, SS
// clock.setDateTime(2016, 12, 9, 11, 46, 00);
}

void getName(){
String date = clock.dateFormat("mdy", dt);
fileName[0] = date[0];
fileName[1] = date[1];
fileName[2] = date[2];
fileName[3] = date[3];
fileName[4] = date[4];
fileName[5] = date[5];
}

void sdCard() {
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}

Serial.println("initialization done.");

getName();

myFile = SD.open(fileName, FILE_WRITE);

if (myFile) {
Serial.print("Writing to file...");
myFile.println("testing 1, 2, 3.");
Serial.println("done.");
}
else {
Serial.println("error opening file");
}
}

void loop()
{
dt = clock.getDateTime();

Serial.print("Long number format: ");
Serial.println(clock.dateFormat("H:i:s", dt));

sdCard();
getName();

myFile = SD.open(fileName, FILE_WRITE);
myFile.println("Testing 1...2...3...")

}

So what I did here was initialize the variable for the text name with placeholders and I used the date in a 6 digit format. Both variables being a string, I can set the first position of the text name to be the first position of the date. So all values are replaced. Should be the same thing for an 8 digit text name, but after that there is a limit, not including the file extension.