Go Down

Topic: SD library: Added new COUNT function (Read 998 times) previous topic - next topic

Jeroi

Mar 27, 2014, 02:23 pm Last Edit: Mar 27, 2014, 06:06 pm by Jeroi Reason: 1
Hello I developed new function to File class that uses exact same mannor than openNextFile but this time it counts all or folders or files in the folder.

You can download edited lib from bottom of the post.

Code: [Select]
class File {
Public:
uint32_t count(char *mode);
}


Modes:
Default: counts all but dots or removed
"FILES": counts only files but dots or removed
"FOLDERS": counts only subfolders but dots or removed

Code: [Select]
uint32_t File::count(char *mode = "") {
 dir_t p;
 uint32_t entries = 0;
 uint32_t folders = 0;
 //Serial.print("\t\treading dir...");
 while (_file->readDir(&p) > 0) {

   // skip deleted entry and entries for . and  ..
   if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
     //Serial.println("dots");
     continue;
   }

   // only list subdirectories and files
   if (!DIR_IS_FILE_OR_SUBDIR(&p)) {
     //Serial.println("notafile");
     continue;
   }

if (DIR_IS_SUBDIR(&p)) {
folders++;
}
entries++;

}

if (mode == "FOLDERS") return folders;
   else if (mode == "FILES") return entries - folders;
   else return entries;

}



Usage:
Code: [Select]

#include <SD.h>

void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT);
SD.begin(10);
int cnt;
File root = SD.open("/");
cnt = root.count("FILES");
Serial.print("File count on SD root: ");
Serial.println(cnt);

}

void loop() {
 
}

Jeroi

I added new version forgot that int has negative values also so renamed function and ints to uint32_t.

Jeroi

I made a fork to Adafruit/SD library and made a Pull request.

The fork: https://github.com/Jeroi/SD

Jeroi

I have found a bug on the counter, it gives 1 too many counts. Till example if I have 20 files on root it will return 21 and so one.

Jeroi

#4
Mar 31, 2014, 06:16 am Last Edit: Mar 31, 2014, 06:26 am by Jeroi Reason: 1
I could neet help for debugging this.

Files in test setup are: 1-20.txt on SDcard root alone.

2 bugs:
- returns 21
- if returning just entries it returns 0, if it returns: entries - folders, then it returns 21.

I have troubles of understanding what causes the issue to be why the counter does count 21. The Dir_t should be read as long as the result is not zero and I have checks for none named ones and dots but still it counts 21.

Code: [Select]
uint16_t File::count(uint8_t mode = 0) {
dir_t p;
uint16_t entries = 0;
uint16_t folders = 0;
//Serial.print("\t\treading dir...");
while (_file->readDir(&p) > 0) {
   
if (p.name[0] == DIR_NAME_FREE) {
//Serial.println("Empty dir name");
break;
}

// skip deleted entry and entries for . and  ..
if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.') {
//Serial.println("dots");
continue;
    }

// only list subdirectories and files
if (DIR_IS_FILE_OR_SUBDIR(&p)) {
if (DIR_IS_SUBDIR(&p)) folders++;
entries++;
    }


}

switch (mode) {
case 1:
return entries - folders;
break;
case 2:
return folders;
break;
default:
return entries;
break;
}
}

Jeroi

Cured these bugs but it accounts for a one new mode for the counter. The counter should be aware of hidden files. The one file too much bug caused by Dropbox hidden file that it was somehow installed itself into the sdhc card. Second bug was missing dirRewind command that caused the function be available for counting only one time. Now after the loop _File->dirRewind(&p) made it cured.

If someone is aware how the sd lib handles hidden files then please point me out.


Jeroi

Okay for now, I think all problems are solved. How ever noticed one problem on internet that there was weard apple spesific files in the sd card. This function will count those files as well as your real files.

Source: https://github.com/Jeroi/SD

Go Up