How do I creat a folder in the sd card with today's date as the name of the folder

I am trying to creat a folder with today's date as the name of the folder, the esp32 will get the date from a GNSS receiver. Inside the folder creat, the esp32 will creat a .txt file and I want to name this file with the current time of the day.
I've tried doing it like this:

void createDirectory() {

  Serial.println("");
  Serial.println("Creating directory");

  // if the folder already exists, don't bother creating it
  if (!sd.exists(Date)) {

    // attempt to access the root directory information
    if (myDir.open("/")) {

      // Create a new folder called 'testFolder'
      // mkdir will return true if it completes successfully
      if (sd.mkdir(Date)) {

        Serial.println(F("Created 'Folder' successfully"));
      
      } else {

        Serial.println("Create 'Folder' failed");
      }
      
    } else {
      
      Serial.println("Open 'Folder' failed");
    }  
    
  } else {

    Serial.println("Folder 'Folder' already exists!");
  }
}

void GetTime(){
   if (millis() - lastTime > 1000)
  {
    lastTime = millis(); //Update the timer

    long latitude = myGNSS.getLatitude();
    Serial.print(F("Lat: "));
    Serial.print(latitude);

    long longitude = myGNSS.getLongitude();
    Serial.print(F(" Long: "));
    Serial.print(longitude);
    Serial.print(F(" (degrees * 10^-7)"));

    long altitude = myGNSS.getAltitude();
    Serial.print(F(" Alt: "));
    Serial.print(altitude);
    Serial.print(F(" (mm)"));

    byte SIV = myGNSS.getSIV();
    Serial.print(F(" SIV: "));
    Serial.print(SIV);

    Serial.println();
    Serial.print(myGNSS.getYear());
    Serial.print("-");
    Serial.print(myGNSS.getMonth());
    Serial.print("-");
    Serial.print(myGNSS.getDay());
    Serial.print(" ");
    Serial.print(myGNSS.getHour());
    Serial.print(":");
    Serial.print(myGNSS.getMinute());
    Serial.print(":");
    Serial.print(myGNSS.getSecond());

    Serial.print("  Time is ");
    if (myGNSS.getTimeValid() == false)
    {
      Serial.print("not ");
    }
    Serial.print("valid  Date is ");
    if (myGNSS.getDateValid() == false)
    {
      Serial.print("not ");
    }
    Serial.print("valid");

    Serial.println();

    long day = myGNSS.getDay();
    long Month = myGNSS.getMonth();
    long Year = myGNSS.getYear();

    Date = (day, Month, Year);
  }
}

and also like this:

void createDirectory() {

  Serial.println("");
  Serial.println("Creating directory");

  // if the folder already exists, don't bother creating it
  if (!sd.exists(Date)) {

    // attempt to access the root directory information
    if (myDir.open("/")) {

      // Create a new folder called 'testFolder'
      // mkdir will return true if it completes successfully
      if (sd.mkdir(Date)) {

        Serial.println(F("Created 'Folder' successfully"));
      
      } else {

        Serial.println("Create 'Folder' failed");
      }
      
    } else {
      
      Serial.println("Open 'Folder' failed");
    }  
    
  } else {

    Serial.println("Folder 'Folder' already exists!");
  }
}

void GetTime(){
   if (millis() - lastTime > 1000)
  {
    lastTime = millis(); //Update the timer

    long latitude = myGNSS.getLatitude();
    Serial.print(F("Lat: "));
    Serial.print(latitude);

    long longitude = myGNSS.getLongitude();
    Serial.print(F(" Long: "));
    Serial.print(longitude);
    Serial.print(F(" (degrees * 10^-7)"));

    long altitude = myGNSS.getAltitude();
    Serial.print(F(" Alt: "));
    Serial.print(altitude);
    Serial.print(F(" (mm)"));

    byte SIV = myGNSS.getSIV();
    Serial.print(F(" SIV: "));
    Serial.print(SIV);

    Serial.println();
    Serial.print(myGNSS.getYear());
    Serial.print("-");
    Serial.print(myGNSS.getMonth());
    Serial.print("-");
    Serial.print(myGNSS.getDay());
    Serial.print(" ");
    Serial.print(myGNSS.getHour());
    Serial.print(":");
    Serial.print(myGNSS.getMinute());
    Serial.print(":");
    Serial.print(myGNSS.getSecond());

    Serial.print("  Time is ");
    if (myGNSS.getTimeValid() == false)
    {
      Serial.print("not ");
    }
    Serial.print("valid  Date is ");
    if (myGNSS.getDateValid() == false)
    {
      Serial.print("not ");
    }
    Serial.print("valid");

    Serial.println();

    Date = (myGNSS.getYear()) + '-' + (myGNSS.getMonth()) + '-' + (myGNSS.getDay());
  }
}

That is not a complete sketch. What is the Date variable these functions are using?

1 Like

the complete sketch is kinda big, so I thought I should only post the important part.
Sry, I forgot to tell what variable it is.
the variable "Date" is a String.

.mkdir() is expecting a C string ( a null terminated array of char) not a String object. Luckily, the String class offers a pointer to the underlying string: c_str()

sd.mkdir(Date.c_str());

1 Like

I suggest using a string (== char array) instead of a String.

char date[11];

sprintf(date, "%04d-%02d-%02d", myGNSS.getYear(), myGNSS.getMonth(), myGNSS.getDay);
1 Like

yep, worked like charm. thanks a lot

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.