Help renaming files with SdFat

Hello everyone. I am not an experienced programmer so sorry for the redundant and ugly code, that's how I managed to get it (kinda) working so far. Also english is not my main language, so sorry for that too.

I'm trying to rename all files in a folder from 0 to x acording with their position. But somehow the function is only renaming the first 3 files correctly, the rest only gets odd numbers as names. Also only the first 3 files are storing correctly in my "list".
Below is the code and result:

char SDfilesCascata[50][15] = {0};

void renameFiles(){
  uint8_t m = 0;
  uint8_t len;
  memset(SDfilesCascata,0,sizeof(SDfilesCascata));
  dir.open("/cascata");

  while (file.openNext(&dir, O_RDWR)){
    file.getName(SDfilesCascata[m],sizeof(SDfilesCascata[m]));
    len = strlen(SDfilesCascata[m]);
    String filename = SDfilesCascata[m];
    if (filename.substring(len-4)==".bmp"||filename.substring(len-4)==".BMP"){
      char newname[6];
      itoa(m,newname,10);
      strcat(newname,".bmp");
      if (!file.rename(&dir,newname)){
        sd.errorHalt("error renaming file bmp");
      }
    }
    else if (filename.substring(len-4)==".jpg"||filename.substring(len-4)==".JPG"){
      char newname[6];
      itoa(m,newname,10);
      strcat(newname,".jpg");
      if (!file.rename(&dir,newname)){
        sd.errorHalt("error renaming file jpg");
      }
    }
    else if (filename.substring(len-4)==".png"||filename.substring(len-4)==".PNG"){
      char newname[6];
      itoa(m,newname,10);
      strcat(newname,".png");
      if (!file.rename(&dir,newname)){
        sd.errorHalt("error renaming file png");
      }
    }
    file.getName(SDfilesCascata[m],sizeof(SDfilesCascata[m]));
    file.close();
    m=m+1;
  }
  CascataNumFiles = m;
  dir.close();
}

The files come out as the attached image.

If I try to run it again, the file.rename() gives error. Also only the first 3 are going to SDFilesCascata[] in the end.

So can someone help me understand why is this happening and how to fix it? Thanks already if you took the time to read this, and even more if you can help me a little :sweat_smile:

Capturar.PNG

Capturar.PNG

nextion_SDcard.ino (5.33 KB)

Without knowing anything about your code and the library, the first thing I noticed is you do not check whether a filename already exists before you call rename.

If that is not the issue, can you provide some more information about your hardware, which exact library you are using and a complete test sketch.

Hello @Klaus_K, thanks for the reply :slight_smile:

Sorry I didn't post the whole code, its full of other stuff not related to this so I didn't think it was necessary, but I'm attaching the .ino file now. The library I'm using is SdFat (GitHub - greiman/SdFat: Arduino FAT16/FAT32 exFAT Library).
I will try checking if the filename already exists, I think maybe this will fix the fact the function can't run a second time , but I dont know about the the error that only gives odd numbers as filenames. Anyway I'll post the results here asap. Thank you so much

Which Arduino are you using?

char SDfilesCascata[50][15] = {0};

A buffer of 750 bytes in addition to the SD library could be too much for your variant.

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