So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Woodsy900
#78843 hey mate,

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.
Code: Select allint 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();
  }
}
User avatar
By RichardS
#78844 How about counting files in all directories with recursion.... :-)

Code: Select allint 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();
  }
}