FileX 如何列出指定目录的文件
RT, FileX 如何列出指定目录的文件,没找到对应的API 凑合看吧:char fn;
fs_open_dir(TARGET_DIR);
result = fs_findfirst(fn);
/* enumerate files */
for (;;)
{
result = fs_findnext(fn);
if (!result) { break; }
}
你可以使用fx_directory_first_full_entry_find设置要打开的文件夹
然后此贴浏览:
ThreadX Filex根目录文件浏览功能实现代码,通过函数fx_directory_next_full_entry_find可实现
http://www.armbbs.cn/forum.php?mod=viewthread&tid=103364&fromuid=58
(出处: 硬汉嵌入式论坛)
eric2013 发表于 2021-3-11 16:41
你可以使用fx_directory_first_full_entry_find设置要打开的文件夹
然后此贴浏览:
你这种方法可以支持递归遍历:) CHAR entry_name;
int ls(char *pathname)
{
UINT status;
UINT attributes;
UINT cnt;
ULONG size;
UINT year;
UINT month;
UINT day;
UINT hour;
UINT minute;
UINT second;
char *path;
if (pathname == FX_NULL)
{
path = FX_NULL;
printf("Directory /:\n");
}
else
{
path = (char *)pathname;
printf("Directory %s:\n", path);
}
// find first
status = fx_directory_first_full_entry_find(get_nand_flash_disk(0),
entry_name,
&attributes,
&size,
&year, &month, &day,
&hour, &minute, &second);
if (status != FX_SUCCESS)
{
return status;
}
/* judge file or directory */
if (attributes & FX_DIRECTORY)
{
printf("%-20s", (char *)entry_name);
printf("%-25s", "<DIR>");
printf("%-25lu\n", size);
}
else
{
printf("%-20s", (char *)entry_name); /* 长文件名 */
printf("%-25lu\n", size);
}
for (cnt = 0; ; cnt++)
{
status = fx_directory_next_full_entry_find(get_nand_flash_disk(0),
entry_name,
&attributes,
&size,
&year, &month, &day,
&hour, &minute, &second);
if (status == FX_NO_MORE_ENTRIES)
{
return FX_SUCCESS;
}
if (status != FX_SUCCESS || entry_name == 0)
{
break;
}
if (entry_name == '.')
{
continue;
}
/* judge file or directory */
if (attributes & FX_DIRECTORY)
{
printf("%-20s", (char *)entry_name); /* 长文件名 */
printf("%-25s\n", "<DIR>");
}
else
{
printf("%-20s", (char *)entry_name); /* 长文件名 */
printf("%-25lu\n", size);
}
}
return status;
}
CHAR entry_name;
int ls(char *pathname)
{
UINT status;
UINT attributes;
UINT cnt;
ULONG size;
UINT year;
UINT month;
UINT day;
UINT hour;
UINT minute;
UINT second;
char *path;
if (pathname == FX_NULL)
{
path = FX_NULL;
printf("Directory /:\n");
}
else
{
path = (char *)pathname;
printf("Directory %s:\n", path);
}
// find first
status = fx_directory_first_full_entry_find(get_nand_flash_disk(0),
entry_name,
&attributes,
&size,
&year, &month, &day,
&hour, &minute, &second);
if (status != FX_SUCCESS)
{
return status;
}
/* judge file or directory */
if (attributes & FX_DIRECTORY)
{
printf("%-20s", (char *)entry_name);
printf("%-25s", "<DIR>");
printf("%-25lu\n", size);
}
else
{
printf("%-20s", (char *)entry_name); /* 长文件名 */
printf("%-25lu\n", size);
}
for (cnt = 0; ; cnt++)
{
status = fx_directory_next_full_entry_find(get_nand_flash_disk(0),
entry_name,
&attributes,
&size,
&year, &month, &day,
&hour, &minute, &second);
if (status == FX_NO_MORE_ENTRIES)
{
return FX_SUCCESS;
}
if (status != FX_SUCCESS || entry_name == 0)
{
break;
}
if (entry_name == '.')
{
continue;
}
/* judge file or directory */
if (attributes & FX_DIRECTORY)
{
printf("%-20s", (char *)entry_name); /* 长文件名 */
printf("%-25s\n", "<DIR>");
}
else
{
printf("%-20s", (char *)entry_name); /* 长文件名 */
printf("%-25lu\n", size);
}
}
return status;
}
yukelab 发表于 2021-4-10 08:53
谢谢分享。 eric2013 发表于 2021-4-11 10:16
谢谢分享。
fx_directory_first_full_entry_find并不能改变扫描的目录吧?需要通过改变默认目录,再执行扫描才行。而且只用fx_directory_next_full_entry_find就可以了其实。 下面函数可以设置子目录搜索。
fx_directory_first_full_entry_find 是查找目录里面的第一条,如果目里面都是文件,那第一条是日期最老的文件么? 我该如何按照日期遍历文件呢? leozhangsd 发表于 2024-4-10 13:16
fx_directory_first_full_entry_find 是查找目录里面的第一条,如果目里面都是文件,那第一条是日期最老 ...
遍历的顺序估计是按照FAT表里面文件存储顺序检索的。
页:
[1]