cancel
Showing results for 
Search instead for 
Did you mean: 

How to count how many files exist in the SD card

FPicc.1
Senior

Hy guys, I want to be able to create a named file in an SD card based on how many files I have in the directory, per example: if I have only 1 file "file1", I'll create the next one as "file2". 

I searched the ff.h library, but didn't find anything that seemed usefull. Has anyone know any ideas I might implement to make it work? 

Also alternatively, is there a function that reads or compares the name of the file and then create another file with a different name?

1 ACCEPTED SOLUTION

Accepted Solutions

IT WORKED,

I added a line to break:

//Count the files in SD and create a new one
  res = f_opendir(&dir, "/");
  if(res==FR_OK)
  {
  for(;;)
  {
    res_dir = f_readdir(&dir, &filinfo);
  if((res_dir==FR_OK)&&(filinfo.fname[0]!= 0))
  {
  file_number++;
  }
  if (res_dir != FR_OK || filinfo.fname[0] == 0) break;
  }
  }
  f_closedir(&dir);
  sprintf(file_name, "file%d.csv", file_number);
 
//Open file to read / create it if it doesn't exist
  fresult = f_open(&fil, file_name, FA_OPEN_ALWAYS | FA_WRITE);
 
Thanks @Tesla DeLorean 

View solution in original post

9 REPLIES 9
AScha.3
Chief II

Hi,

Just read the filenames (++ a counter) until end of dir , then you know how many files in this dir .

(I tested : f_readdir needs about 0,5..1ms , so for 30 files need about 30ms .)

Then make a string with "fileX" (X = ++counter) and open file with this new name, thats it.

.

If you feel a post has answered your question, please click "Accept as Solution".

FatFs is designed to be small, there's not specific function to count files, not that I recall one in STDIO or WIN32 either..

Enumerate files via f_opendir() / f_readdir(), filter out directories, and perhaps count FILExxxx.TXT files, or search for largest used case, and increment to the next.

 

http://elm-chan.org/fsw/ff/doc/readdir.html

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi @AScha.3 

I've writed the code to do it, it creates a file"0", even tho there already have a file in the SD. But it doesn't create another one, it keeps updating the file0. Here's the code: 

 

//Count the files in SD and create a new one
  res_dir = f_readdir(&dir, &filinfo);
  if((res_dir==FR_OK)&&(filinfo.fname[0]!= 0))
  {
  file_number++;
  }
  sprintf(file_name, "file%d.csv", file_number);
 
//Open file to read / create it if it doesn't exist
  fresult = f_open(&fil, file_name, FA_OPEN_ALWAYS | FA_WRITE);
 
Have you noticed something or see anything wrong?

Does it count files?

What error does f_open() return?

I cited specific code showing how to count files / directories on a specific path.

It's not hard to corrupt the file system if writing fails or if you lose power, etc.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Actually no, it isn't entering in the if loop.

The res_dir return FR_INVALID_OBJECT.

I added this:

res = f_opendir(&dir, path);
if(res==FR_OK)
{
res_dir = f_readdir(&dir, &filinfo);
if((res_dir==FR_OK)&&(filinfo.fname[0]!= 0))
{
file_number++;
}
}

And now res is returning FR_INVALID_DRIVE

Solved the issue on the errors by putting: res = f_opendir(&dir, "/");

It created a file1 (it already had file0), but it continues not counting after that, it only updates file1

Right, because you don't loop.

The cited example uses for (;;) it could use a while() variant

It has to iterate though all the files/directories within the object, it has to do this one at a time until all the content is consumed.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Like this?

 res = f_opendir(&dir, "/");
  if(res==FR_OK)
  {
  for(;;)
  {
    res_dir = f_readdir(&dir, &filinfo);
  if((res_dir==FR_OK)&&(filinfo.fname[0]!= 0))
  {
  file_number++;
  }
  }
  }
  f_closedir(&dir);
 
It worked, counted the files, but after that it got stuck in the:     res_dir = f_readdir(&dir, &filinfo); 
and didn't move forward

IT WORKED,

I added a line to break:

//Count the files in SD and create a new one
  res = f_opendir(&dir, "/");
  if(res==FR_OK)
  {
  for(;;)
  {
    res_dir = f_readdir(&dir, &filinfo);
  if((res_dir==FR_OK)&&(filinfo.fname[0]!= 0))
  {
  file_number++;
  }
  if (res_dir != FR_OK || filinfo.fname[0] == 0) break;
  }
  }
  f_closedir(&dir);
  sprintf(file_name, "file%d.csv", file_number);
 
//Open file to read / create it if it doesn't exist
  fresult = f_open(&fil, file_name, FA_OPEN_ALWAYS | FA_WRITE);
 
Thanks @Tesla DeLorean