bool Workspace::DeleteDirectory(CString directoryPath) { CFileFind finder; // build a search string with wildcards CString searchString(directoryPath); searchString += _T("\\*.*"); // start searching for files BOOL bSearching = finder.FindFile(searchString); while (bSearching) { bSearching = finder.FindNextFile(); //skip . and .. files, as they represent thecurrent directory and parent directory //markers while iterating through files. Otherwise an infinte loop would occur. if (finder.IsDots()) { continue; } // if it's a directory, recursively delete the files if (finder.IsDirectory()) { CString directoryPath = finder.GetFilePath();//condense DeleteDirectory(directoryPath); } else { //delete the file //CT2A converts CString to const char* if(remove(CT2A(finder.GetFilePath()))) { //0 is success so file removal has failed AfxMessageBox(_T("File: ") + finder.GetFilePath() + _T(" could not be removed")); finder.Close(); return false; } } } //not searching anymore as all files have been deleted, so delete the directory SetCurrentDirectory(_T("C:\\")); if(!RemoveDirectory(directoryPath)) { AfxMessageBox(_T("Directory: ") + directoryPath + _T(" could not be removed")); finder.Close(); return false; } else { finder.Close(); return true; } }
SHFileOperation
FO_DELETE
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)