SD Card: dynamic file name

this is how I did that:

way up at the beginning:

File dataFile;                      // variable for SD
char filename[]  = "00000000.CSV";  // filemame array for datalogger
char logString[] = "000000000000";  // build a string for the event log

this:

char logString[] = "000000000000"; // build a string for the event log

is not needed for deriving the file name, it is just included to make the rest of the code understandable

code to set up SD card not included

the routine that derives the filename is called every time an event is logged. it gets the date from LocalTime: time_t - the offset from UTC and the offset for DST if necessary:

///////////////////////////////////////////////////////////////////////////////////////////////
// SD & DATALOGGER FUNCTIONS //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

void getFilename( time_t LocalTime )
     {          
      sprintf(filename, "%04d%02d%02d", year(LocalTime), month(LocalTime), day(LocalTime));
     }
           
void logEvent(int sw)
{
 time_t eventTime = LocalTime();
      {
       getFilename( eventTime );
       lcd4.setCursor(3,0); lcd4.print("Filename:"); lcd4.print(  filename );
       File dataFile = SD.open(filename, FILE_WRITE);
       if (dataFile)                                                   // if the file is available, write to it:
         {
          lcd4.setCursor(2,0); lcd4.print("O");
          sprintf(logString, "%02d,%02d,%02d,%02d", sw,
          hour(eventTime), minute(eventTime), second(eventTime));
          dataFile.println(logString); 
          dataFile.close();                                           // close the file after writing
          lcd4.setCursor(2,0); lcd4.print("*"); lcd4.setCursor(9,1); lcd4.print(logString);
         }
       
 /*     dataFile = SD.open(filename);
       
       if (dataFile)                                                  // if the file is available, read from it:
       {
         while (dataFile.available()) 
         {
          Serial.write(dataFile.read());
         }
         { 
          dataFile.close();
          Serial.print( "data file "); Serial.print(filename); Serial.println( " closed after reading" );
         }
       }        
       
       else 
       {
        Serial.print("error opening "); Serial.println(filename);                  //if the file isn't open, pop up an error:     
       }*/
 
   }

}

this:

lcd4.setCursor(3,0); lcd4.print("Filename:"); lcd4.print(  filename );
lcd4.setCursor(2,0); lcd4.print("*"); lcd4.setCursor(9,1); lcd4.print(logString);

is a status display and an easter egg, not necessary for deriving the filename. the file status is preceded by "SD:" on the display.

the part between /* & */ was for troubleshooting and verifying. not needed, but there is no benefit to throwing it away

sw is the loop counter for the loop that reads the sensor array.

note that

char filename[] = "00000000.CSV"; // filemame array for datalogger

is 12 characters and it works just fine all day every day