天天看點

MFC删除某個檔案夾下的所有目錄檔案

1、該函數是删除檔案夾下的所有檔案

http://bbs.csdn.net/topics/390448664

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;

}