C语言递归删除所有空文件夹,包括文件名称有空格

C语言递归删除所有空文件夹,包括文件名称有空格,第1张

直接上代码:
int delete_folder_file(const char dir)
{
intptr_t handle;
struct _finddata_t findData;
char dirNew[500];
strcpy_s(dirNew, strlen(dir) + 1, dir);
strcat_s(dirNew, strlen(dirNew) + 4, “\*”);
if ((handle = _findfirst(dirNew, &findData)) == -1)
{
printf(“Failed to findfrist file”);
return -1;
}
while (_findnext(handle, &findData) == 0)
{
//findData.attrib & _A_SUBDIR 为0 说明不是文件夹
if (findData.attrib & _A_SUBDIR)
{
//strcmp 比较,如果相等就是0
if (strcmp(findData.name, “.”) == 0 || strcmp(findData.name, “…”) == 0)
//printf(“delete_empty_folder: % s\n”, dirNew);
continue;
strcpy_s(dirNew, strlen(dir) + 1, dir);
strcat_s(dirNew, strlen(dirNew)+ 3, “\”);
strcat_s(dirNew, strlen(dirNew) + strlen(findData.name) + 1, findData.name);
delete_folder_file(dirNew);
}
else
{
continue;
}
//重组路径
int x = 0, z = 0;
char y[100] = { 0,0 }; //最终删除地址
while (dirNew[x] != ‘\0’)
{
if (dirNew[x] == ‘\’ && x == 2) //判断D:后面的那个/
{
strcat(y, “\”“);
z++;
}
else
{
y[z] = dirNew[x];
}
z++;
x++;
}
char del[500] = {‘r’, ‘d’, ’ ', ‘/’, ‘q’, ’ '};
strcat(y, “””);
strcat_s(del, 500, y);
printf("%s ", y);
printf(“delete empty folder: %s\n”, del);
system(del);
}
_findclose(handle);
return 0 ;
}
*

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1323998.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-12
下一篇2022-06-12

发表评论

登录后才能评论

评论列表(0条)

    保存