Setting a Log file name from the date of RTC DS1307

I am having some issue with trying to generate a log file as the date .txt so something like 20130214.txt

I have tried to call it from a char but no luck it does not create the file.

char file_today_name[30];



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



void logEvent(const char *msg) {

  // create or open a file for append
  ofstream sdlog( file_today_name,  ios::out | ios::app);

  // append a line to the file
 sdlog << day() << "/" << month() << "/" << year() << "," << hour() << ":" << minute() << ":" << second() << ","  << msg << endl;

  // check for errors
  if (!sdlog) sd.errorHalt("append failed");

  sdlog.close();
}

Perhaps http://snippets-r-us.com could help you with the snippets. Here, strange as it may seem, we need to see all of your code.

The sprintf() function bears investigation, though. Creating the name string from the tear, month, and day, in a global variable, is one line of code.

sprintf(file_today_name, "%04d%02d%02d.txt", year(), month(), day());
3 Likes

Paul, I could just kiss you :slight_smile:

thank you so much it worked a treat

void logEvent(const char *msg) {

  // create or open a file for append
  sprintf(file_today_name, "%04d%02d%02d.txt", year(), month(), day());
  ofstream sdlog( file_today_name ,  ios::out | ios::app);

  // append a line to the file
 sdlog << day() << "/" << month() << "/" << year() << "," << hour() << ":" << minute() << ":" << second() << ","  << msg << endl;

  // check for errors
  if (!sdlog) sd.errorHalt("append failed");

  sdlog.close();
2 Likes

Here is a snippet from my project. It is used to change the .CSV filename of the on-board backup on the first trip through the loop after midnight.

/*

 //  Serial print commands are for PLX-DAQ
 
 From cosm library example and lifts from a lot of others
 particularly from Stanley in Kuala Lumpur.
 Use your own DS18B20 addresses, keys etc.
 */
 
#include <DallasTemperature.h>   // Dallas temp 
#include <OneWire.h>             // Dallas Temp 
#include <Ethernet.h>            // Ethernet 
#include <SPI.h>                 // Ethernet               
#include <HttpClient.h>          // Cosm lib
#include <Cosm.h>                // Cosm lib
#include <PCD8544.h>             // Nokia 5110
#include <SD.h>                  // SD card, and is all it needs
#include <string.h>              // from "Date As Filename"
#include "RTClib.h"              // from "Date As Filename"
#include "Wire.h"                // Original RTC lib for LCD, SD, serial
                                 // This is also used by 
#define DS1307_ADDRESS 0x68

RTC_DS1307 RTC;
static PCD8544 lcd;

File myFile;
char filename[] = "00000000.CSV";

// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};

byte InThermo[8] =  {
  0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8] = {
  0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {
  0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95}; 

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

int  second, minute, hour, weekDay, monthDay, month, year;

unsigned int frac;

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";

char calcId1[] = "diff";
char calcId2[] = "flowRate";
char calcId3[] = "kW";


void setup() {
  lcd.begin(84, 48);
     // Register the custom symbols...
  lcd.createChar(DEGREES_CHAR, degrees_glyph);
  lcd.createChar(SLASH_CHAR, slash_glyph);
  Wire.begin();
  Serial.begin(9600);
  Serial.print("    filename   ");
  delay(300);//Wait for newly restarted system to stabilize
  lcd.setCursor (0,0);
  lcd.print("Initializing");
  delay(2000);
  lcd.setCursor (0,1);

  pinMode(10, OUTPUT);

  if (!SD.begin(4)) 
  {
    lcd.print("failed!");
    delay (2000);
    return;
  }
  lcd.print("init. OK!");
  delay(2000);
      getFileName();
      Serial.println(filename);
  lcd.clear();

  Serial.println("LABEL,Time,InTemp,OutTemp,diff,DrainTemp");

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
  sensors.setResolution(DrainThermo, 12);

  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();

  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }
  
  pinMode(sensorPin, INPUT);
  digitalWrite(sensorPin, HIGH);

  pulseCount        = 0;
  flowRate          = 0.0;
  flowLitres   = 0;
  totalLitres = 0;
  oldTime           = 0;

  // The Hall-effect sensor is connected to pin 17 (A3) which uses interrupt 0.
  // Configured to trigger on a FALLING state change (transition from HIGH
  // state to LOW state)
  attachInterrupt(sensorInterrupt, pulseCounter, FALLING);
}

void loop() 
{
    GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
    Daily = kWh - Daily;
    MaxRise = 0;
  }

}  // loop ends here

void getFileName(){

  DateTime now = RTC.now();

  filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
  filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
  filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
  filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
  filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
  filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
  filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
  filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
}

I have one last issue I have a value of unitid[0]= 1000; and a - that I would like to add to the start of the file name.

this code work well to put text file as a date

sprintf(file_today_name, "%04d%02d%02d.txt", year(), month(), day());

If I change the value to

sprintf(file_today_name, "%04d_%04d%02d%02d.txt",unitid[0], year(), month(), day());

if I even drop off the under score it still does not work

sprintf(file_today_name, "%04d%04d%02d%02d.txt",unitid[0], year(), month(), day());

FaT16, the file structure used on SD cards only supports 8.3 format names. That is 8 characters for the file name and 3 for the extension. Your date string is 8 characters.

Paul, once again thankyou for all your assistance.

I just have to change what my file name is to suit the 8 characters :slight_smile: