删除多级非空目录收藏

来源:互联网 发布:网络一会掉一会连 编辑:程序博客网 时间:2024/05/23 13:19

void DeleteDir(CString str)
{
 CFileFind finder;      //文件查找类
 CString strdel,strdir; //strdir:要删除的目录,strdel:要删除的文件
 strdir=str+"//*.*";    //删除文件夹,先要清空文件夹,加上路径,注意加"//"
 BOOL b_finded=(BOOL)finder.FindFile(strdir);
 while(b_finded)
 {
  b_finded=(BOOL)finder.FindNextFile();
  if (finder.IsDots())  continue;//找到的是当前目录或上级目录则跳过
  strdel=finder.GetFileName(); //获取找到的文件名
  if(finder.IsDirectory())   //如果是文件夹
  {
   strdel=str + "//" + strdel;//加上路径,注意加"//"
   DeleteDir(sock,strdel); //递归删除文件夹
  }
  else //不是文件夹
  {
   strdel=str + "//" + strdel;
   if(finder.IsReadOnly())//清除只读属性
   {   
    SetFileAttributes(strdel,GetFileAttributes(strdel)&(~FILE_ATTRIBUTE_READONLY));
   }
   DeleteFile(strdel); //删除文件(API)
  }
 }
 finder.Close();
 RemoveDirectory(str); //删除文件夹(API)
}

原创粉丝点击