Your code was pretty spot on... you just missed one tiny little thing I think...
I tested it without this addition and it came back 0 but with the addition it shows the correct number of files..
So in the below code you have X ++; I believe it should be X++; (no space between the X and the +). I am currently working on a project that has SD card and the need to print the file names to screen. Hope it works if you hadnt already figured it out.
int CountFiles(File dir) {
int X = 0;
while (true) {
File entry = dir.openNextFile();
if (! entry) {
return X;
}
//Serial.print(entry.name());
if (entry.isDirectory()) {
} else {
// files have sizes, directories do not
X++; /* This was changed from X ++; Note the space*/
//Serial.print("\t\t");
//Serial.println(entry.size(), DEC);
}
entry.close();
}
}
int CountFiles(File dir) {
int X = 0;
while (true) {
File entry = dir.openNextFile();
if (! entry) {
return X;
}
//Serial.print(entry.name());
if (entry.isDirectory()) {
X += CountFiles(entry);
} else {
// files have sizes, directories do not
X++; /* This was changed from X ++; Note the space*/
//Serial.print("\t\t");
//Serial.println(entry.size(), DEC);
}
entry.close();
}
}
https://epartsconnect.com
Live realtime stock market prediction website. https://www.stocksignalslive.com my latest creation. Uses AI and machine learning.
New site featuring ESP8266 and ESP32 projects. tutorials, and news.
http://EverythingESP.com
ESP8266.com Founder and Administrator
The Mind Factory (get more from your 3D printer)
Home of the ESP8266, ESP8285, and ESP32 microcontrollers....