递归删除一个目录

来源:互联网 发布:js预览上传图片 编辑:程序博客网 时间:2024/06/10 04:07

CFindFile的使用框架:

void Recurse(LPCTSTR pstr){   CFileFind finder;   // build a string with wildcards   CString strWildcard(pstr);   strWildcard += _T("\\*.*");   // start working for files   BOOL bWorking = finder.FindFile(strWildcard);   while (bWorking)   {      bWorking = finder.FindNextFile();      // skip . and .. files; otherwise, we'd      // recur infinitely!      if (finder.IsDots())         continue;      // if it's a directory, recursively search it      if (finder.IsDirectory())      {         CString str = finder.GetFilePath();         TRACE(_T("%s\n"), (LPCTSTR)str);         Recurse(str);      }   }   finder.Close();}



递归删除

//循环删除一个目录void RecursiveDelete(CString strDir){CFileFind ff;CString strPath;strPath = strDir;if (strPath.Right(1) != '\\'){strPath += '\\';}strPath += "*.*";BOOL bWorking = ff.FindFile(strPath);while (bWorking){bWorking = ff.FindNextFile();// skip . and .. files; otherwise, we'd// recur infinitely!if (ff.IsDots())continue;// if it's a directory, recursively search itif (ff.IsDirectory()){//递归目录CString str = ff.GetFilePath();TRACE(_T("%s\n"), (LPCTSTR)str);RecursiveDelete(str);//删除目录::SetFileAttributesA(str, FILE_ATTRIBUTE_NORMAL);::RemoveDirectory(str);}else{//删除文件CString str = ff.GetFilePath();TRACE(_T("%s\n"), (LPCTSTR)str);::SetFileAttributes(str, FILE_ATTRIBUTE_NORMAL);::DeleteFile(str);}}ff.Close();}int main(int argc, char *argv[]){RecursiveDelete("C:\\20_128\\");return 0;}


原创粉丝点击