Dynamic filepath to SD card file

Hi all,

I'm trying to save data to files on an SD card. The SD card works fine and I can save to a text file in the root directory. But I would like to save to a file in a subdirectory.

My function that saves the temperature is below. I can't get the following line to behave:

String logfilePath = "\\logs_tmprt\\tmprt_" + getFormattedDate() + ".txt";

The directory "logs_tmprt" is in the root of the SD card.

What am I missing?

Thanks for reading!

void logTemperature(){

  // dateTime format is YY MM DD HH MM
  unsigned long dateTime = (year() - 2000) * 100000000 +              // 22 00 00 00 00             
                           month() * 1000000 +                        // 22 07 00 00 00
                           day() * 10000 +                            // 22 07 23 00 00
                           hour() * 100 +                             // 22 07 23 18 00
                           minute();                                  // 22 07 23 18 52

  // Add temperature
  String logEntry = String(dateTime) + String(getTemperature());                // logEntry: 2207231853274 = 22.07.23 18:53 27.4ºC

  // Path to logfile as string
  //String logfilePath = getFormattedDate() + ".txt";       // <<< This works
  
  // This does not work:
  //String logfilePath = "logs_tmprt\\tmprt_" + getFormattedDate() + ".txt";    // getFormattedDate returns a string "2022.07.23"
  String logfilePath = "\\logs_tmprt\\tmprt_" + getFormattedDate() + ".txt";    
  
  // Convert logfilePath to character array
  char logfilePathCharArray[logfilePath.length() + 1];
  logfilePath.toCharArray(logfilePathCharArray, (logfilePath.length() + 1));
  
  // Open file or create if it doesn't exist
  File logFile = SD.open(logfilePathCharArray, FILE_WRITE);                               
  // Save logEntry to the file
  logFile.println(logEntry);
  // Close the file
  logFile.close();
  
} // end void logTemperature

Are you able to use names longer than 8.3 in this circumstance?

I googled on a hunch and found reason enough to ask.

a7

Maybe rewrite this to:

  File logFile = SD.open(logfilePath.c_str(), FILE_WRITE);                               

It says here that forward slashes should be used.

E.g.,

String logfilePath = "/logs_tmprt/tmprt_" + getFormattedDate() + ".txt";

@jfjlaros Yes indeed, that's it.

Changing the back slashes () to forward slashes (/) did the trick.
String logfilePath = "logs_tmprt/tmprt_" + getFormattedDate() + ".txt";
Seems like I don't need the first "/" ? I'll try both ok.

I'll also try the "logfilePath.c_str()" suggestion. Saves a few lines and looks nicer.

But the code works fine now, thanks!

@alto777 I guess you mean if I can use "moreThenEightCharacters.txt" as a filename?
The filename saves correctly as "tmprt_2022.07.09.txt". So that would be 15.3 and yes that works.

Which SD library are you using ?

Depends what your current directory is when you call it. Preceding with a / will always be with reference to the root directory. Without the / the path will be applied to the current directory. If your current directory happens to be the filesystem root then both versions will find the same file, but if your code happens to change directory, your logging process may end up looking for /current/directory/logs_tmprt/tmprt_"...etc instead. If the logs are always in /logs_imprt/, then its probably best to precede with / so you know that the process is looking to save or write to the correct file in he correct location.

1 Like

@UKHeliBob Yes, that might indeed be relevant. I'm using a Teensy 4.1 and this SD library:
https://github.com/PaulStoffregen/SD
(and 15.3 should be 16.3)

@BitSeeker Thanks for the info:
Preceding with a / will always be with reference to the root directory.

I can confirm that the following code works (on a Teensy 4.1 using the mentioned library):

void logTemperature(){

  unsigned long dateTime = (year() - 2000) * 100000000 +                                  // 22 00 00 00 00             
                           month() * 1000000 +                                            // 22 07 00 00 00
                           day() * 10000 +                                                // 22 07 23 00 00
                           hour() * 100 +                                                 // 22 07 23 18 00
                           minute();                                                      // 22 07 23 18 52

  String logEntry = String(dateTime) + String(getTemperature());                          // logEntry: 2207231852274 22.07.23 18:53 27.4ºC
  
  String logfilePath = "/logs_tmprt/tmprt_" + getFormattedDate() + ".txt";

  File logFile = SD.open(logfilePath.c_str(), FILE_WRITE);                                // Open file or create if it doesn't exist
  logFile.println(logEntry);
  logFile.close();
}

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