看了好多朋友用那个f_unlink()函数没法删除还有内容的文件夹,今天在网上找到一个不错的函数,所以拿来跟大家分享一下~废话不多说直接上函数的代码:
/* 删除文件夹内所有的文件和文件夹*/
FRESULT FS_DeleteIntFile(TCHAR* path)
{
UINT i, j;
FRESULT res;
DIR dir;
FILINFO fno;
#if _USE_LFN
fno.lfname = 0; /* Set null pointer because LFN is not needed */
#endif
res = f_opendir(&dir, path);
if (res == FR_OK)
{
for (i = 0; path[ i]; i++) ;
path[i++]='/';
for (;;)
{
res = f_readdir(&dir, &fno);
if (res != FR_OK || !fno.fname[0]) break;
if (fno.fname[0] == '.') continue;
j = 0;
do
path[i+j] = fno.fname[j];
while (fno.fname[j++]);
if (fno.fattrib & AM_DIR)
{
res = FS_DeleteIntFile(path);
if (res != FR_OK) break;
}
res = f_unlink(path);
if ((res != FR_OK) && (res != FR_DENIED)) break;
}
path[--i] = '\0';
}
return res;
}
/* 删除此文件夹及内部的文件及文件夹*/
FRESULT FS_DeleteFolderOrFile(TCHAR* path)
{
FRESULT res;
res = FS_DeleteIntFile(path);
if (res == FR_OK)
{
res = f_unlink(path);
}
else if (FR_NO_PATH == res)
{
res = f_unlink(path);
}
return res;
}
首先把这两个函数的定义放在ff.c当中,别忘了在ff.h中声明一下。然后直接调用就行啦
至于怎么用,不用我说了吧~
|