//////////////////////////////////////////////////////////////////////////--zwh--注:删除当前文件夹及其子文件夹与文件(接口);
BOOL CBackupToolDlg::clearFiles(CString strPath)
{
//============================功能概要:删除给定路径下的所有文件======================//
/*
* 功能:传入绝对路径,清空此文件夹内的所有内容(做为接口使用,故建议不在此函数内递归);
*/
//===================================================================================//
return clearSubDirFiles(strPath);
}
//////////////////////////////////////////////////////////////////////////--zwh;
//////////////////////////////////////////////////////////////////////////---zwh--注:递归法遍历并删除当前目录下的所有文件及文件夹;
BOOL CBackupToolDlg::clearSubDirFiles(CString strPath)
{
if (strPath.Right(1)!="\\")
{
strPath += "\\";
}
strPath += "*.*"; //形如c:\windows\system32\*.*
CFileFind fileFinder;
BOOL bFile = fileFinder.FindFile(strPath.GetBuffer());
while(bFile)
{
bFile = fileFinder.FindNextFile();
if (!fileFinder.IsDots()) //当为文件夹时;
{
CString strFileName = fileFinder.GetFileName();
CString strTemp = strPath;
int i = strTemp.Find("*.*");
strTemp.Delete(i,3);
CString strFileFullName = strTemp + strFileName;
if(fileFinder.IsDirectory())
{
clearSubDirFiles(strFileFullName);
}
else
{
DeleteFile(strFileFullName.GetBuffer());
}
}
}
fileFinder.Close();
CString temp = strPath;
int pos = temp.Find("\\*.*");
temp.Delete(pos,4);
if(!RemoveDirectory(temp.GetBuffer()))
{
return FALSE;
}
else
{
return TRUE;
}
}
//////////////////////////////////////////////////////////////////////////--zwh;
评论