递归地删除目录

4 篇文章 0 订阅

写程序时用到一个删除目录的功能,但C库函数提供的_rmdir() 只能删除空目录,在网上搜索了一下也没得到满意的答案,于是就自己写了个小程序,递归地删除目录下的内容,最后删除目录本身。

主要用到了以下函数:

_access(); /*判断目录是否存在*/

_chdir(); /*改变当前工作目录*/

_findfirst(); /*在目录下查找第一个文件或文件夹*/

_findnext(); /*在目录下查找下一个文件或文件夹*/

_findclose(); /*结束一个查找操作*/

remove(); /*删除一个文件*/

_rmdir(); /*删除一个空目录*/


代码如下(下面的程序将直接删除文件或文件夹,而不是移到回收站,虽然已经本人多次测试,但为了安全起见,准备调试下面程序的朋友请做好备份,或将删除文件和文件夹的函数替换为输出文件名)

  1. #include "io.h"  
  2. #include "direct.h"  
  3.   
  4. #include <iostream>  
  5. using std::cin;  
  6. using std::cerr;  
  7. using std::cout;  
  8. using std::endl;  
  9.   
  10. #include <string>  
  11. using std::string;  
  12.   
  13.   
  14.   
  15. void empty_directory(/*const char* dirName*/);  
  16.   
  17. int main()  
  18. {  
  19.     string path;  
  20.     cout << "Input name of the directory you want to remove: ";  
  21.     cin >> path;  
  22.   
  23.     if (_access(path.c_str(), 06) == -1)  
  24.     {  
  25.         cerr << "error: directory does not exist." << endl;  
  26.         exit(-1);  
  27.     }  
  28.   
  29.     if (path.at(path.length() - 1) != '\\')  
  30.     {  
  31.         path += '\\';  
  32.     }  
  33.   
  34.     _chdir(path.c_str());  
  35.   
  36.     empty_directory();  
  37.   
  38.     _chdir(path.c_str());  
  39.     _chdir("..");  
  40.     _rmdir(path.c_str());  
  41.   
  42.   
  43.     return 0;  
  44. }  
  45.   
  46.   
  47.   
  48. void empty_directory( /*const char* dirName*/ )  
  49. {  
  50.     _finddata_t fileinfo;  
  51.     memset(&fileinfo, 0x0, sizeof(fileinfo));  
  52.   
  53.   
  54.     intptr_t iFind = _findfirst("*", &fileinfo);  
  55.     if (iFind == -1)  
  56.     {  
  57.         cerr << "error: _findfirst failed." << endl;  
  58.         return;  
  59.     }  
  60.   
  61.     /* process the first item */  
  62.     if (fileinfo.name[0] != '.')  
  63.     {  
  64.         if ((fileinfo.attrib & _A_SUBDIR) != 0)  
  65.         {  
  66.             /* process directory */  
  67.             _chdir(fileinfo.name);  
  68.             empty_directory(/*fileinfo.name*/);  
  69.             _chdir("..");  
  70.             _rmdir(fileinfo.name);  
  71.               
  72.         }  
  73.         else  
  74.         {  
  75.             /* process file */  
  76.             remove(fileinfo.name);  
  77.         }  
  78.     }  
  79.   
  80.   
  81.     while (_findnext(iFind, &fileinfo) == 0)  
  82.     {  
  83.         if (fileinfo.name[0] != '.')  
  84.         {  
  85.             if ((fileinfo.attrib & _A_SUBDIR) != 0)  
  86.             {  
  87.                 /* process directory */  
  88.                 _chdir(fileinfo.name);  
  89.                 empty_directory(/*fileinfo.name*/);  
  90.                 _chdir("..");  
  91.                 _rmdir(fileinfo.name);  
  92.             }  
  93.             else  
  94.             {  
  95.                 /* process file */  
  96.                 remove(fileinfo.name);  
  97.             }  
  98.         }  
  99.   
  100.     }  
  101.   
  102.   
  103.   
  104.   
  105.     _findclose(iFind);  
  106.       
  107.   
  108. }  

程序在vs2008下测试正常。

转载:http://blog.csdn.net/selina6874/article/details/6784766


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值