硬汉嵌入式论坛

 找回密码
 立即注册
查看: 25|回复: 0
收起左侧

[FatFs] fatfs递归一个文件夹下的所有文件和子文件

[复制链接]

1万

主题

7万

回帖

12万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
122563
QQ
发表于 5 小时前 | 显示全部楼层 |阅读模式
https://elm-chan.org/fsw/ff/doc/readdir.html


[C] 纯文本查看 复制代码
/* Recursive scan of all items in the directory */

FRESULT scan_files (
    char* path        /* Start node to be scanned (***also used as work area***) */
)
{
    FRESULT res;
    DIR dir;
    UINT i;
    static FILINFO fno;


    res = f_opendir(&dir, path);                   /* Open the directory */
    if (res == FR_OK) {
        for (;;) {
            res = f_readdir(&dir, &fno);           /* Read a directory item */
            if (fno.fname[0] == 0) break;          /* Break on error or end of dir */
            if (fno.fattrib & AM_DIR) {            /* The item is a directory */
                i = strlen(path);
                sprintf(&path[i], "/%s", fno.fname);
                res = scan_files(path);            /* Enter the directory */
                if (res != FR_OK) break;
                path[i] = 0;
            } else {                               /* The item is a file. */
                printf("%s/%s\n", path, fno.fname);
            }
        }
        f_closedir(&dir);
    }

    return res;
}


int main (void)
{
    FATFS fs;
    FRESULT res;
    char buff[256];


    res = f_mount(&fs, "", 1);
    if (res == FR_OK) {
        strcpy(buff, "/");
        res = scan_files(buff);
    }

    return res;
}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2026-5-15 20:21 , Processed in 0.252181 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表