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
nextion_SDcard.ino (5.33 KB)