16,472
社区成员
发帖
与我相关
我的任务
分享
void DeleteFileFolder(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);
DeleteFileFolder(str);
RemoveDirectory(str);
}
}
finder.Close();
}
DeleteFileFolder(_T("F:\\TEST"));
void CFileOperationDlg::OnBnClickedOk()
{
if (DeleteFileFolder(L"D:\\testFolder"))
{
MessageBox(L"删除空文件夹成功!");
}
}
bool CFileOperationDlg::DeleteFileFolder(CString filePath)
{
CFileFind finder;
//try
{
if(filePath.Right(1) != "\\")
filePath += "\\";
filePath += "*.*";
TRACE( filePath );
TRACE( "\r\n" );
BOOL bWorking= finder.FindFile(filePath);
if (bWorking==0)
{
LPSTR templp;
WideCharToMultiByte(CP_ACP,0,(LPCWSTR)filePath,-1,templp,0,NULL,0);
//templp=filePath.GetBuffer(0);
RemoveDirectoryA(templp);
}
while(bWorking)
{
bWorking=finder.FindNextFile();
if ( finder.IsDots() )
continue;
if (finder.IsDirectory())
{
DeleteFileFolder(finder.GetFilePath());
}
}
}
finder.Close();
return true;
}