Pages: [1]   Go Down
Author Topic: SD library: Added new COUNT function  (Read 979 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
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:
#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() {
  
}

* SD_v2.zip (56.79 KB - downloaded 12 times.)
« Last Edit: March 27, 2014, 12:06:06 pm by Jeroi » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
}
}
« Last Edit: March 30, 2014, 11:26:56 pm by Jeroi » Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 12
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Pages: [1]   Go Up
Jump to: