[sdfat] why chdir return an error while it works

Hello
I am writting a example file. My goal is to save some measurements in json line format into a folder path as the following

/log/year/month/day/

then the log for today will be

/log/2024/7/5/log.jsonl

I have two problems and I will discribe the first with the SD card.

First, I have a function defineVolumeWorkingDirectory() which will define the Volume Working Directory

I have create antoher function goToVolumeWorkingDirectory() to move into the Volume Working Directory, where the log.jsonl file will be created to save the log (json format)

I am surpise to see that the file is create in /LOG/2024/7/5/log.jsonl and the log is saved there. But...

The sd.chdir() does not return me an error.

However, the sd.chdir(sd_pathLog) return false and the following text is printed

.. Failed to chdir the VWD

How can it be possible if the log.jsonl file is created with the content???

Is there a way to debug if(sd.chdir(sd_pathLog)) to try why it return false while the file is well created a the right location????

Update: if(sd.mkdir(sd_pathLog)) return true

Are you using SdFat.h?

Hello,
Yes, I do. But in that example, he chdit just one folder.
May be I should chdir one folder after each

chdir('log');
chdir(year);
chdir(month);
chdir(day);

Maybe two "slants"?

//log//year//month//day//

The date_time array is too small, may be corrupting the file path or some other variables.

char date_time[18];                     //1010-10-10 11:11:11

< edit > the infamous semi-colon after the if statement

432         bool b = sd.chdir(sd_pathLog);
433         if(b == false);
434         {
435           if(debug)
436           {
437             Serial.print(F(".. "));
438             Serial.println(F("failed to chdir the VWD"));
439           }
440           return -2;
441         }

Hello David,
Ho ye, shame on me. thanks for this

I solved my problem, but honestly I do not know how :slight_smile:

I corrected the @david_2018 observation, but I am not sure this cause the issue.

However, I modify this

int defineVolumeWorkingDirectory()
{
  DateTime now = rtc.now();
  int16_t yy = now.year();                                        // Save the year
  int16_t mm = now.month();                                       // Save the month
  int16_t dd = now.day();                                         // Save the day

  sprintf(sd_pathLog,"/LOG/%i/%i/%i/",yy,mm,dd);                  // sd_pathLog is a global variable

  return volumeWorkingDirectory(false);
}

to

int defineVolumeWorkingDirectory()
{

  sprintf(sd_pathLog,"/LOG/%i/%i/%i",y,m,d);                  // sd_pathLog is a global variable

  return volumeWorkingDirectory(false);
}

y,m and d are global variable used all around my code RtcInterval update thoses global variable at each loop()
I also removed the last / at sd_pathLog.

PS: I removed the function defineVolumeWorkingDirectory() and printf(sd_pathLog,"/LOG/%i/%i/%i",y,m,d); is now in checkVolumeWorkingDirectory(). In that way, after midnight, the log are saved into the next day folder.

The semicolon after the if statement on line 433 ends the if statement (in effect, if b is equal to false, then do nothing).
The bracketed code on lines 434 through 441 is executed unconditionally, regardless of the value of b.