cancel
Showing results for 
Search instead for 
Did you mean: 

SD Card

nechi
Associate III
Posted on March 30, 2017 at 18:49

Hi, 

I'm using the cubeF4 to work on FATFS application and i'm searching for a function in FATFS library that can return the content of the SD Card: number of files and their names.  

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
nechi
Associate III
Posted on April 03, 2017 at 15:25

Thank you all, all your suggestions were helpful 

I saved files in a directory created in the SD Card and i just used the function '

f_readdir' 

into a loop to count files in my directory

f_opendir (&dir,'mydirectory');

while (1)

{

   res_dir = f_readdir(&dir, &fno);

   if((res_dir==FR_OK)&&(fno.fname[0]!= 0))

   {

      nb_file_dir++;

   }

}

View solution in original post

9 REPLIES 9
Posted on March 30, 2017 at 19:12

You'll have to write such code, the FATFS library provides functions like f_opendir, f_readdir, you'd use these to process and count files in the current directory, and you'd need recurse and descend into the directories you encounter at each level.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Imen.D
ST Employee
Posted on March 30, 2017 at 19:21

Hi,

There is FatFs_uSD example on STM32CubeF4 firmware package that can help you to use most of the features offered by FatFs and to configure a microSD drive.

You can get help from the User manual UM1721'Developing Applications on STM32Cube with FatFs'

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
Posted on March 31, 2017 at 10:11

Thank you, i'll understand more FATFS functions 

Posted on March 31, 2017 at 10:13

Thank you Imen, i'm already working with the 

FatFs_uSD example, i'll see this manual.

john doe
Lead
Posted on April 01, 2017 at 03:15

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

the code example doesn't print the count, but it does walk the directory tree and print the contents

nechi
Associate III
Posted on April 03, 2017 at 15:25

Thank you all, all your suggestions were helpful 

I saved files in a directory created in the SD Card and i just used the function '

f_readdir' 

into a loop to count files in my directory

f_opendir (&dir,'mydirectory');

while (1)

{

   res_dir = f_readdir(&dir, &fno);

   if((res_dir==FR_OK)&&(fno.fname[0]!= 0))

   {

      nb_file_dir++;

   }

}

Posted on April 03, 2017 at 16:31

That will also count sub-directories, you should also check the attributes.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
nechi
Associate III
Posted on April 11, 2017 at 18:02

My SDcard is 8GB with one directory where i save files from the F4 discovery and it works well. Now i faced some problems, i added an other directory (2.6Gbyte) in the sd card and it's not related to the first one. when i save new files in the first directory i notice that the old ones are overwritten with 'UUUUUU...'. Did you know this issue please? I can't understand why they are modified even though i didn't opened any of them with the stm32 i just count the number of the existing files in the directory and then i create new ones  

Hi @nechi 

I did pretty much the same thing, but I want to create a file with the name of how much files there are in the directory, for that reason I didn't put the code in the while. Here's the code:

 

//Count the files in SD and create a new one
  res = f_opendir(&dir, "/");
  if(res==FR_OK)
  {
  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);
 
file_number only goes up to 1 and doesn't count the rest, it only updates 'file1'. Do you have any ideia of what can I do?