且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MFC删除某个文件夹下的所有目录文件

更新时间:2021-07-16 08:05:06

1、该函数是删除文件夹下的所有文件
BOOL CMainFrame::DeleteDirectory(const CString &strPath)
{
CFileFind tempFind;
TCHAR sTempFileFind[MAX_PATH] = { 0 };

wsprintf(sTempFileFind, _T("%s\\*.*"), strPath);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);

while (IsFinded)
{
IsFinded = tempFind.FindNextFile();

if (!tempFind.IsDots())
{
TCHAR sFoundFileName[200] = { 0 };
_tcscpy(sFoundFileName, tempFind.GetFileName().GetBuffer(200));

if (tempFind.IsDirectory())
{
TCHAR sTempDir[200] = { 0 };
wsprintf(sTempDir, _T("%s\\%s"),strPath, sFoundFileName);
DeleteDirectory(sTempDir); //删除文件夹下的文件
RemoveDirectory(sTempDir); //移除空文件
}
else
{
TCHAR sTempFileName[200] = { 0 };
wsprintf(sTempFileName, _T("%s\\%s"), strPath, sFoundFileName);
DeleteFile(sTempFileName);
}
}
}

tempFind.Close();

// if(!RemoveDirectory(strPath))
// return false;

return true;
}