issue creating a file name structure using sprintf

Hi,
I am trying to create a file name to write to an SD card. The following works - a file is created on the SD card;

//Construct a file name and open file
  char str[24];
  static int k = 0;
  File outFile;

  k = k + 1;
  
  sprintf( str, "%d%d%d_%d%d.jpg",_RTC.getYear() , _RTC.getMonth(), 
          _RTC.getDay(), _RTC.getHours(), k);

  Serial.print(F("File string> ")); Serial.println(str);
  
  //Open the new file
  outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
  while (!outFile) {Serial.println(F("File open failed"));delay(1000);}
  Serial.println(F("File opened"));

The following doesn’t work - responds with "File open failed" and no file is created;

//Construct a file name and open file
  char str[24];
  static int k = 0;
  File outFile;

  k = k + 1;
  
  sprintf( str, "%d%d%d_%d%d.jpg",_RTC.getYear() , _RTC.getMonth(), 
          _RTC.getDay(), _RTC.getHours(), _RTC.getMinutes());

  Serial.print(F("File string> ")); Serial.println(str);
  
  //Open the new file
  outFile = SD.open(str, O_WRITE | O_CREAT | O_TRUNC);
  while (!outFile) {Serial.println(F("File open failed"));delay(1000);}
  Serial.println(F("File opened"));

The only difference is that k in the first example is replaced with _RTC.getMinutes() in the second example. I have no idea why this should be, it seems to me that they should both work.

Any help with this is much appreciated.

Cheers.

Can we see the serial prints?

Is your file name growing larger than the 8.3 limitation?

HI,
And thanks for the replies - much apprciated.

Hi aarg, The following is the serial run for the the structure that works (using the variable k instead of _RTC.getMinutes():

Serial monitor opened...
ArduCAM Start!
SPI interface OK.
ACK CMD OV2640 detected.
setup RTC...Unix time = 1591400462
5/6/20	23:41:02
SD Card detected.

start Capture
Capture Done.
fifo length: 64520
move FIFO image to SD card
File string> 2065_231.jpg     //23hrs and k=1
File opened
Image save OK.

start Capture
Capture Done.
fifo length: 64520
move FIFO image to SD card
File string> 2065_232.jpg     //23hrs and k=2 etc
File opened
Image save OK.

start Capture
Capture Done.
fifo length: 65544
move FIFO image to SD card
File string> 2065_233.jpg
File opened
Image save OK.

The following is the serial run for the the structure that doesnt work (using _RTC.getMinutes():

Serial monitor opened...
ArduCAM Start!
SPI interface OK.
ACK CMD OV2640 detected.
setup RTC...Unix time = 1591400462
5/6/20	23:41:02
SD Card detected.

start Capture
Capture Done.
fifo length: 60424
move FIFO image to SD card
File string> 2065_2341.jpg    //32 hrs and 41 minutes
File open failed
File open failed
File open failed

Hi cattledog, yes it does look like the 8.3 limitation may be the issue. Initially i tried to create a date stamped directory to hold time stamped files but i had a heap of issues getting the SD lib to reliably create a subdirectory under the root and then to reliably put the time stamped files into the date stamped directory. I kept having trouble with file corruption.

Is there a better way to do this?

What is the correct/accepted way to create a sub directory then write files to that sub-directorty? I had a look in the arduino reference for the SD lib but it wasn’t much help.

Any help is much appreciated.

Cheers.

Is there a better way to do this?

SdFat.h supports long file names and you can switch from SD.h to that library.

If you are going to use dates and times as part of the filename, it will help tremendously if you include leading zeros on the numbers, such that June 5th appears as 0605 instead of 65, that will keep the file names a consistent length and keep the files in order when sorting on the filename.

An easier method would be to use the epoch time divided by 60, that will give you a unique number for each minute, and will fit the 8.3 format.

Indeed your current scheme is very ambiguous:

21115_234

could be

2021-11-05
2021-01-15
2002-11-15

and 02:34 or 23:04

ISO standard date format is the one to go for, it sorts alphabetically with time, so

YYYY-MM-DDThh:mm

For instance 2021-11-15T02:34.jpg

If ':' is problematic in a filename use '-', or simply squash all the digits together:

20211150234.jpg

For instance 2021-11-15T02:34.jpg

Is there not an 8.3 restriction on the filename ?

Hi,
And thanks for the replies - much appreciated.

David_2018: i like the idea of using epoch time / 60 however the people that are going to be making use of the created images will be comparing those images to text data that is also being recorded which uses time stamped data inside a date stamped file.

Cattledog: is the SDfat lib a drop in replacement for the SD lib i.e. can i use my current SD lib based code but just change to the SDfat lib. The examples i have looked at do suggest this but are there any "gotchas" in its use compared to the SD lib?

MarkT: Yes i do need to go with the ISO convention and will probably go with squashing all together as you suggest.

Again thanks for all your help.

Cheers.

Cattledog: is the SDfat lib a drop in replacement for the SD lib i.e. can i use my current SD lib based code but just change to the SDfat lib. The examples i have looked at do suggest this but are there any "gotchas" in its use compared to the SD lib?

I don't think it is just a simple change of the #include. However SdFat is a faster and includes more capabilities. It may be worth your time learning how to use it.

UKHeliBob:
Is there not an 8.3 restriction on the filename ?

If there is you cannot name files by date-and-time anyway. SdFat would be the library to choose for
this.

With 8.3 sequential numbering you can divide by date into directories, and then name files in those directories by time.