Skip to main content
Visitor II
January 22, 2024
Solved

How to count how many files exist in the SD card

  • January 22, 2024
  • 2 replies
  • 3227 views

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?

    This topic has been closed for replies.
    Best answer by FPicc.1

    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 

    2 replies

    Super User
    January 22, 2024

    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.

    .

    FPicc.1Author
    Visitor II
    February 2, 2024

    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?
    Graduate II
    February 2, 2024

    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.

    Graduate II
    January 22, 2024

    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