前言
加一个遍历文件夹的功能。
代码
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);
}