Problem opening file on SD card w/ Uno and Adafruit Datalogger

I am working on a datalogger that uses the UNO and Adafruit datalogger shield and I am having an issue when opening a file to an SD card. I am using the RTC to get the date info in order to name a .csv file. The file name info is saved to an array and prints out fine but as a block, (i.e "YYYMMDD.csv") but when I add dashes to format the file name better ("YYYY-MM-DD.csv") it no longer writes the file to the SD card but the file name prints out no problem so it is being saved to the array. Is this an invalid file name format or am I writing to the array the wrong way? Here is the code I have been using. Thanks in advance for your help.

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10;

// the logging file
File dataFile;
String dataFilename = "";
char fn[] = "YYYY-MM-DD.CSV";

// Function to call time stamp for naming the log file.
void dateTime(uint16_t* date, uint16_t* time) {
  DateTime now =;

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(),;

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());

void error(char *str)
  Serial.print("error: ");
  // red LED indicates error
  digitalWrite(redLEDpin, HIGH);


void setup(void)
  // connect to RTC
  if (!RTC.begin()) {
    dataFile.println("RTC failed");
    Serial.println("RTC failed");
   // initialize the SD card
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
  Serial.println("card initialized.");
    // following line sets the RTC to the date & time this sketch was compiled
  // RTC.adjust(DateTime(__DATE__, __TIME__));
  DateTime now =;

  //  Formats and adds the year to the file name string
    if (now.year() < 10) {
      dataFilename = String(dataFilename + '0' + String(now.year(), DEC));
    else {
    dataFilename = String(dataFilename +String(now.year(), DEC));

  // Add the dash after the year info
  dataFilename = String(dataFilename + '-');

 //  //  Formats and adds the month to the file name string
   if (now.month() < 10) {
      dataFilename = String(dataFilename + '-' + '0' + String(now.month(), DEC));
    else {
    dataFilename = String(dataFilename + '-' + String(now.month(), DEC));
// Add second dash to file name string 
dataFilename = String(dataFilename + '-');
 //  Formats and adds the day to the file name string
 if ( < 10) {
      dataFilename = String(dataFilename + '-' +  '0' + String(, DEC));
    else {
    dataFilename = String(dataFilename + '-' + String(, DEC));
   dataFilename = String(dataFilename + ".CSV");
  // Serial.println(dataFilename);
  // Serial.println(dataFilename.length());
    for (int i=0;i<=dataFilename.length();i++) {
    fn[i] = dataFilename.charAt(i);
  Serial.print ("LOOK HERE");
  if (! SD.exists(fn)) {
      // only open a new file if it doesn't exist
     dataFile =, FILE_WRITE); 
     break;  // leave the loop!
  dataFile =, FILE_WRITE);

File names are limited to 8 characters, a dot, and three character extension.

The first method is fine, a human can read it and the scope is wide open.

If you want to encode in the name, Unix seconds is 8 hex chars long. A computer can turn that into a date up to some time in 2038, to the second.

Do you know that DOS files carry time and date info that can be looked up and displayed?

The SD library doesn't support it but the last update Time and Date are 2 16 bit values per file in the Directory.

DOSFAT time is by bit fields, which C handles. The top 5 bits is hour, the next 6 bits are minute which leaves 5 bits so it's every 2 seconds, not second.

DOSFAT date is by bit fields. The top 7 bits is how many years since 1980. Next 4 bits is month, next 5 bits is day.

I get this from my 1992 DOS Programmers Reference 3rd Edition.

IIRC the FILE object you make through SD open() should have all that data. If you can find the file name, time is 2 bytes at (name start + 16) and date is the next 2 bytes.