-->
Page 1 of 1

SD Card File Count issue

PostPosted: Fri Apr 07, 2023 9:55 am
by beic
Hi there,

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.

Code: Select allvoid 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;
}

Re: SD Card File Count issue

PostPosted: Wed Apr 12, 2023 7:25 am
by rooppoorali
Somebody faced the same problem with this same function when using an Arduino. You can check this thread:

https://forum.arduino.cc/t/error-when-s ... les/629753