sdfat counting files

hi,

is there an easy way to count files in a folder on a sd card. I'm using the sdfat libary.
On my sd card are many folders withe max. 40 files ore less. I only want to count *.dat files.

The directoryFunctions example that comes with the SdFat library shows how to count the number of files at the root level. It really shouldn't be that difficult to do that for any directory. Counting only files with a specific extension is not a difficult addition.

i saw this example, but i don’t understand it.

#include <SPI.h> 
#include "SdFat.h"

// SD card chip select pin.
const uint8_t SD_CHIP_SELECT = 4;

SdFat sd;

SdFile file;

ArduinoOutStream cout(Serial);

#define error(msg) sd.errorHalt(F(msg))

void setup() {
  Serial.begin(9600);  
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }
  cout << F("\nList of files on the SD.\n");
  sd.ls(LS_R);
  
}

void loop() {
  // put your main code here, to run repeatedly:

}

this code shows all the all files in each directory on the serial . But how can i get a int array with the number of files in each directory?

why doesn’t work this code:

#include <SPI.h> 
#include "SdFat.h"

// SD card chip select pin.
const uint8_t SD_CHIP_SELECT = 4;

SdFat sd;

SdFile file;

#define error(msg) sd.errorHalt(F(msg))

const int cMin=1;     //first directory "01"
const int cMax=6;     //last directory "06"               
unsigned int numberOfFiles[200];  //number of files in each directory


void countFiles(char folder, int index){
  int c=1;
  char fileName;
  while (1){
    fileName=strcat(c,".txt");
    if (sd.exists(fileName)) c++; 
    else break;              
  }
  numberOfFiles[index]=c;
}


void setup() {
  Serial.begin(9600);  
  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) {
    sd.initErrorHalt();
  }
  char dirName;
  int i=0;
  for (i = cMin; i < cMax; i++){
  }
    sprintf(dirName,"%02i",i);  
    Serial.print(dirName);
    countFiles(dirName,i);
    Serial.print(numberOfFiles[i]);
}

void loop() {
  // put your main code here, to run repeatedly:

}

my sd card looks like this:

directory 01…06
in each directory 2 files

but it shows that: “16!£”

can anyone help me?

thanks

this code shows all the all files in each directory on the serial . But how can i get a int array with the number of files in each directory?

The ls() method does not care what you want. You need to re-implement the method in your code, so you can get what you want.

  char fileName;
  while (1){
    fileName=strcat(c,".txt");

How many characters can you store in a char? Does the number ONE ring a bell?

Does it really make any sense to pass strcat() anything other than an array as the first argument? Not to me, it doesn't.

But, lets suppose that, through shear luck, that all worked.

Now, suppose that there are 2 text files in the directory, named 1.txt and 6.txt.. The file 1.txt exists, so you increment c. The file 2.txt doesn't exist, so you break out of the loop, and record that there is one file in the directory. That is wrong.

Now, suppose that the two files are named 2.txt and 6.txt. 1.txt does not exist, so you record that there is one file in the directory. That isn't right, either.

You’re right.
I tried some other code and i think this could work:

#include <SPI.h> 
#include "SdFat.h"

const uint8_t SD_CHIP_SELECT = 4;

SdFat sd;

SdFile file;


void setup() {
  Serial.begin(9600);
  int fileCount = 0;
  //sd.chdir("/01");
  sd.vwd()->rewind(); 
  while (file.openNext(sd.vwd(), O_READ)) {
    if (!file.isHidden()) {
      fileCount++;
    }
    file.close();
  }
  Serial.print(fileCount);
}

void loop() {
  // put your main code here, to run repeatedly:

}

it’s from the example of sdfat.
But i get always a 0 and in the root dir are 3 files.

oh, i just forgot to initialize the sd card. Now i get a 9.
3 fiels and 6 folders.
But i just want to know the number of files.

how can i count only *.txt?
can you give me a lead to modify the code?

  while (file.openNext(sd.vwd(), O_READ)) {
    if (!file.isHidden()) {
      fileCount++;
    }
    file.close();
  }
  Serial.print(fileCount);

There is a method to determine if the file is a directory. If it is, don't count it.

There is a method to get the name of the file. Get it, and make a copy of it. Determine if the extension is ".txt" (strstr() is one way). If it isn't, don't count it.