VC删除文件夹下所有文件

//删除文件夹目录(非空)

  1. bool DeleteDirectory(char* sDirName)   
  2. {   
  3.     CFileFind tempFind;   
  4.     char sTempFileFind[200] ;  
  5.       
  6.     sprintf(sTempFileFind,"%s\*.*",sDirName);   
  7.     BOOL IsFinded = tempFind.FindFile(sTempFileFind);   
  8.     while (IsFinded)   
  9.     {   
  10.         IsFinded = tempFind.FindNextFile();   
  11.           
  12.         if (!tempFind.IsDots())   
  13.         {   
  14.             char sFoundFileName[200];   
  15.             strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));   
  16.               
  17.             if (tempFind.IsDirectory())   
  18.             {   
  19.                 char sTempDir[200];   
  20.                 sprintf(sTempDir,"%s\%s",sDirName,sFoundFileName);   
  21.                 DeleteDirectory(sTempDir);   
  22.             }   
  23.             else   
  24.             {   
  25.                 char sTempFileName[200];   
  26.                 sprintf(sTempFileName,"%s\%s",sDirName,sFoundFileName);   
  27.                 DeleteFile(sTempFileName);   
  28.             }   
  29.         }   
  30.     }   
  31.     tempFind.Close();   
  32.     if(!RemoveDirectory(sDirName))   
  33.     {   
  34.         return FALSE;   
  35.     }   
  36.     return TRUE;   
  37. }   


/////
//下面是应用,CString m_strDir 是一个文件夹路径,如:d:downloadpic

  1. BOOL DelAll()  
  2. {  
  3.     if(PathFileExists(m_strDir))       
  4.         DeleteDirectory((LPSTR)(LPCTSTR)m_strDir);  
  5.     return 1;  
  6. }  

如果不进行递归删除。你可以使用API函数SHFileOperation,它可以一次删除目录及其下面的子目录和文件。

示例代码:

  1. <pre name="code" class="cpp">BOOL DelTree(LPCTSTR lpszPath)  
  2.   
  3. {  
  4.   
  5.    SHFILEOPSTRUCT FileOp;  
  6.   
  7.    FileOp.fFlags = FOF_NOCONFIRMATION;  
  8.   
  9.    FileOp.hNameMappings = NULL;  
  10.   
  11.    FileOp.hwnd = NULL;  
  12.   
  13.    FileOp.lpszProgressTitle = NULL;  
  14.   
  15.    FileOp.pFrom = lpszPath;  
  16.   
  17.    FileOp.pTo = NULL;  
  18.   
  19.    FileOp.wFunc = FO_DELETE;  
  20.   
  21.     returnSHFileOperation(&FileOp) == 0;</pre><br>  
  22. <br>  
  23. <pre></pre>  
  24. <p></p>  
  25. <p>}</p>  
  26. <p> </p>  
  27. <pre></pre>  
  28. <pre></pre>  

转自:http://blog.csdn.net/wangjieest/article/details/7000640
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值