Trouble with creating folders to store files on an SD card

Hi (again),

I feel like everything is 2 steps forward one step back at the moment.

I'm basically trying to make a datalogger that logs accelerometer data once the ultrasonic sensor detects and object. Everything works fine if I just create a new file in the root folder each time there is a detection.

As soon I I introduce a daily folder to store each set of data for that day it starts to misbehave. (I'm using folders because I'm using an esp32 and sdFat isn't supported and so I have to use short filenames.)

The first set of data gets logged fine and the serial shows me the file path like this...


Once it tries to write a new file to the folder it creates the following path...


However if I reset the device it'll work as I want it but just the once, so it'll create a new file in the correct folder.

The double slash (//) seems to be a esp32-ism, I need one in the actual file path and one in the name for it work for it to write a file. Even if I want to just write to the root I still need the /.

If anyone can help me figure this out it would be appreciated.

I've simplified the recordData() as it's not the focus as it works fine.

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

//-SD Card-
#define SD_CS = 5;
File dataFile;

char fileName[20];//Time of vessel arrive time
char folderName[20]; //Dated folder for saving data
char file[20];//store file name
char folder[20];//store folder name
char fileDir[20];//full file path
char filePath[20];//full file path

void setup() {


  //SD Card
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));



void loop() {

  if (distanceInMM <= 500) {




void createFileName() {

  DateTime now =;

  int nowHour = now.hour();
  int nowMinute  = now.minute();

  sprintf(fileName, "/%02i_%02i.csv", nowHour, nowMinute);


  strcpy(file, fileName);


void createNewFolder() {

  DateTime now =;

  int nowYear = now.year();
  int nowMonth = now.month();
  int nowDay =;

  sprintf(folderName, "/%02i%02i%02i", nowYear - 2000, nowMonth, nowDay);

  strcpy(folder, folderName);


  /*Serial.println(nowYear - 2000);

void getFilePath() {


  strcat(fileDir, "/");
  strcat(fileDir, folder);
  strcat(fileDir, "/");
  strcat(fileDir, file);

  strcpy(filePath, fileDir);


void getAccelData() {
  //Accel Sensor code in here

void recordData(){



          dataFile =, FILE_WRITE);



Now I thought I could use SD.exist() then just write straight into the folder, but it doesn't seem to find the folder name, maybe because it's an array?

Thanks :slight_smile:

here's a code fragment i use to create and read a file on esp32

#include "Arduino.h"

#include "cfg.h"
#include "eng.h"
#include "utils.h"
#include "vars.h"

const char *cfgFname = "/spiffs/koala.cfg";

// ...
            cfgSave (cfgFname);
// ...

// ---------------------------------------------------------
cfgLoad (
    const char *filename )
    int res = 1;
    printf ("%s: %s\n", __func__, filename);

    FILE * fp = fopen (filename, "rb");
    if (NULL == fp)  {
        perror ("cfgLoad - fopen");
        res = 0;
        goto done;

    // verify TLV file format
    byte magic [sizeof(cfgMagic)];

    if (1 != fread ((void*) & magic [0], sizeof(magic), 1, fp))  {
        perror ("cfgLoad - fread hdr\n");
        res = 0;
        goto done;

    if (strncmp ((char*)magic, "TLV", sizeof(cfgMagic)))  {
        printf (" %s: non-TLV file\n", __func__);
        res = 0;
        goto done;

    // read TLVs
    CfgHdr_s hdr;
    CfgVar_s *p;

    while (fread ((void*) &hdr, sizeof(hdr), 1, fp))  {
        if (0 == hdr.size)

        if (NULL == (p = _cfgFindVar (  {
            printf (" %s: unknown cfgVar id, %d", __func__,;

 //     printf (" %s: %s\n", __func__, p->desc);

        // read amount written specified by hdr.size
        int nread = fread ((void*) p->p, hdr.size, 1, fp);
        if (! nread)  {
            printf (" %s: fread incomplete, %d %d",
                        __func__, nread, p->nByte);
            perror (" cfgLoad - fread");
            res = 0;
            goto done;

    fclose (fp);
    return res;

// ---------------------------------------------------------
#if 1
cfgSave (
    const char *filename )
    printf ("%s: %s\n", __func__, filename);

    FILE * fp = fopen (filename, "wb+");
    if (NULL == fp)  {
        perror ("cfgSave - fopen");

    if (1 != fwrite ((void*) cfgMagic, sizeof(cfgMagic), 1, fp)) {
        perror ("cfgSave: fwrite cfgMagic");
        goto done;

    CfgHdr_s  hdr;
    for (CfgVar_s *p = cfgVarTbl; V_NONE != p->type; p++)  {   = p->id;
        hdr.size = p->nByte;

        if (1 != fwrite ((void*) & hdr, sizeof(hdr), 1, fp)) {
            perror ("cfgSave: fwrite hdr");
            goto done;

        int nwr = fwrite ((void*) p->p, p->nByte, 1, fp);
        if (! nwr)  {
            printf (" %s: fwrite incomplete, %d %d",
                        __func__, nwr, p->nByte);
            perror (" cfgSave - fwrite");

    fclose (fp);

You forgot to set fileDir to the empty string before building it:

You could change this to:

Also is 20 chars enough for the full path? What if you later change to 8.3 file name and 8 char directory name?

1 Like

Mark! Thanks mate! This had me going crazy over 2 nights believe it or not. I polished off a bottle of Kraken and still didn't find the solution. Thanks for your time, it's appreciated.

With regards to the 20 chars, it seems to be enough? I've increased it to 50, but out of curiosity does making the array bigger than it needs to be have any detrimental effects?

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