Skip to main content
mfrank9
Associate III
Jul 12, 2021 1:43 PM UTC
Solved

FATFS: show all files

Hi,

is it possibility to show a list of files and folders with the fatfs library?

Like "ls" in the console.

This topic has been closed for replies.
Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Jul 12, 2021 5:20 PM UTC

The FatFs library provides you with the means to implement this, but not the code, although you might find some if you look.

Typically a problem that can be solved via recursive, or queued linear, traversal

For a simple single level

puts("Display Directory");
 
 {
 DIR dir;
 char *path;
 UINT BytesWritten;
 char string[128];
 
 path = ""; // where you want to list
 
 res = f_opendir(&dir, path);
 
#ifdef DBG
 if (res != FR_OK)
 printf("res = %d f_opendir\n", res);
#endif
 
 if (res == FR_OK)
 {
 while(1)
 {
 FILINFO fno;
 
 res = f_readdir(&dir, &fno);
 
#ifdef DBG
 if (res != FR_OK)
 printf("res = %d f_readdir\n", res);
#endif
 
 if ((res != FR_OK) || (fno.fname[0] == 0))
 break;
 
 sprintf(string, "%c%c%c%c %10d %s/%s",
 ((fno.fattrib & AM_DIR) ? 'D' : '-'),
 ((fno.fattrib & AM_RDO) ? 'R' : '-'),
 ((fno.fattrib & AM_SYS) ? 'S' : '-'),
 ((fno.fattrib & AM_HID) ? 'H' : '-'),
 (int)fno.fsize, path, fno.fname);
 
 puts(string);
 }
 }
 }

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

4 replies

SBEN .2
Visitor II
Jul 12, 2021 4:26 PM UTC

Hello @mfrank9​ ,

It is possible to recursively travers the file tree using FATFS and display its content using f_readdir(). A limited example, for inspiration, can be found in the implementation of kStorage_GetDirectoryFiles() (See here) in the STM32G474E-EVAL demonstration project.

Best regards,

@SBEN .2​ 

Tesla DeLorean
Tesla DeLoreanBest answer
Guru
Jul 12, 2021 5:20 PM UTC

The FatFs library provides you with the means to implement this, but not the code, although you might find some if you look.

Typically a problem that can be solved via recursive, or queued linear, traversal

For a simple single level

puts("Display Directory");
 
 {
 DIR dir;
 char *path;
 UINT BytesWritten;
 char string[128];
 
 path = ""; // where you want to list
 
 res = f_opendir(&dir, path);
 
#ifdef DBG
 if (res != FR_OK)
 printf("res = %d f_opendir\n", res);
#endif
 
 if (res == FR_OK)
 {
 while(1)
 {
 FILINFO fno;
 
 res = f_readdir(&dir, &fno);
 
#ifdef DBG
 if (res != FR_OK)
 printf("res = %d f_readdir\n", res);
#endif
 
 if ((res != FR_OK) || (fno.fname[0] == 0))
 break;
 
 sprintf(string, "%c%c%c%c %10d %s/%s",
 ((fno.fattrib & AM_DIR) ? 'D' : '-'),
 ((fno.fattrib & AM_RDO) ? 'R' : '-'),
 ((fno.fattrib & AM_SYS) ? 'S' : '-'),
 ((fno.fattrib & AM_HID) ? 'H' : '-'),
 (int)fno.fsize, path, fno.fname);
 
 puts(string);
 }
 }
 }

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
mfrank9
mfrank9Author
Associate III
Jul 20, 2021 9:29 AM UTC

Thank you!

That works.

The following lines don't do what they're supposed to, but I don't know why.

This is not a problem as they work when called individually.

 sprintf(string, "%c%c%c%c %10d %s/%s",
 ((fno.fattrib & AM_DIR) ? 'D' : '-'),
 ((fno.fattrib & AM_RDO) ? 'R' : '-'),
 ((fno.fattrib & AM_SYS) ? 'S' : '-'),
 ((fno.fattrib & AM_HID) ? 'H' : '-'),
 (int)fno.fsize, path, fno.fname);

TBode.1
Visitor II
Jan 26, 2023 9:55 AM UTC

Thanks!

Helped me much.