Enumerating FAT filesystem tree

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Not applicable
I am trying to familiarise myself with the FATFS002 file system functions.

This is the function I am trying to use to enumerate the filesystem tree:

void print_dirs(const char* dir, int depth) {
DIR d;
d.fs = 0;

f_opendir(&d, dir);

FILINFO finfo;
f_readdir(&d, NULL);

while (f_readdir(&d, &finfo) == FR_OK && d.sect != 0) {

int plen = depth * 2 + strlen(finfo.fname) + 3; //+3 for \r\n\0
char p[plen];
sprintf(p, "%-*s\r\n", depth * 2, finfo.fname);
USBD_VCOM_SendString(p);

if (finfo.fattrib & AM_DIR) {
int nlen = strlen(dir) + strlen(finfo.fname) + 2; //plus two for slash and trailing \0
char n[nlen];
sprintf(n, "%s\\%s", dir, finfo.fname);
print_dirs(n, depth + 1);
}
}
}


I call print_dirs(".",0); and it finds the directory DIR0 on the sdcard and then recurses into print_dirs(".\DIR0", 1); but f_opendir(&d, ".\DIR0"); open the root dir - it behaves as if I'd called f_opendir(&d, "."); again. Can anybody tell me what I'm doing wrong?
0 Likes
0 Replies