I'm trying to count Files on the SD Card, but for some reason the "getFileCount" function is always returning 0 value.
The weird thing is that the Config.txt file is loading without any issue.
Note: There is no Directory on the SD Card.
Any help would be appreciated.
void SDcardInit(int sPin)
{
if (SD.begin(sPin)) {
dbgCustomMsg(F("SD Card Init ... OK"));
myDelay(200);
sd_File_Count = getFileCount(SD);
myDelay(200);
ConfigLoad(); // The Config.txt is loading without any issue
} else {
dbgCustomMsg(F("SD Card Init ... ERR"));
while (1) myDelay(100);
return;
}
}
int getFileCount(SDClass& sd)
{
char rootPath[] = "/";
if (!sd.exists(rootPath)) {
Serial.println("Root directory not found!");
return 0;
}
File root = sd.open(rootPath);
if (!root) {
Serial.println("Failed to open root directory!");
return 0;
}
int fileCount = 0;
while (true) {
File entry = root.openNextFile();
if (!entry) {
break;
}
fileCount++;
entry.close();
}
root.close();
return fileCount;
}