【esp32-s3】6.4 文件系统——遍历文件夹

前言

加一个遍历文件夹的功能。

代码

void dir_list(char *path)
{
    char *dir_name;
    if(path != NULL)
        dir_name = path;
    else
        dir_name = SD_MOUNT_POINT;//"/t1";
        
    ESP_LOGI(TAG, "Opening dir %s", dir_name);

    DIR *dir_t1 = opendir(dir_name);

    int count = 0;
    char* names[DIR_NUM_MAX];

    printf("DIR: %s\n",dir_name);
    while(1) {
        struct dirent* de = readdir(dir_t1);
        if (!de) {
            break;
        }
        
        names[count] = char_change_A2a(de->d_name);

        printf("\t%s\n", de->d_name);
        // printf("\t%s type = %d\n", de->d_name, de->d_type);

        // dir
        if(de->d_type == 2)
        {
            strcpy(dir_temp,dir_name);
            strcat(dir_temp,"/");
            strcat(dir_temp,de->d_name);
            printf("this is dir:%s\n",dir_temp);

            char * temp_cpr;
            temp_cpr = "system~1";
            // printf("%s,%s",de->d_name , temp_cpr);
            if(strcmp(de->d_name,temp_cpr)==0)
                printf("ignore this dir\n");
            else
            {
                dir_list(dir_temp);
            }
        }

        // search wav file 
        char* search_key = ".wav";
        // printf("search wav = %s\n",strstr(de->d_name,search_key));
        if(strstr(de->d_name,search_key) != NULL)
        {
            // printf("this is wav file: %s\n",de->d_name);
            int file_name_len = strlen(dir_name)+strlen(de->d_name);
            // printf("file name len = %d\n",file_name_len);
            char *temp_path = (char *)malloc(sizeof(char) * (file_name_len));
            strcpy(temp_path,dir_name);
            strcat(temp_path,"/");
            strcat(temp_path,de->d_name);
            printf("file path = %s\n",temp_path);

            struct wavinfo *p1 = NULL;
            p1 = (struct wavinfo*)malloc(1);
            p1->name = temp_path;
            list[current_list_num]=*p1;
            current_list_num++;
            printf("p1->name = %s\n",p1->name);
        }

        ++count;
    }

    closedir(dir_t1);
}

结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值