bool FileMisc::MoveFolder(const TCHAR* szSrcFolder, const TCHAR* szDestFolder, BOOL bIncludeSubFolders, const TCHAR* szFileMask, HANDLE hTerminate, BOOL bProcessMsgLoop) { if (CopyFolder(szSrcFolder, szDestFolder, bIncludeSubFolders, szFileMask, hTerminate, bProcessMsgLoop)) { // don't pass on hTerminate to ensure the operation completes DeleteFolderContents(szSrcFolder, bIncludeSubFolders, szFileMask, NULL, bProcessMsgLoop); return true; } return false; }
bool FileMisc::RemoveFolder(const TCHAR* szFolder, HANDLE hTerminate, BOOL bProcessMsgLoop) { // if the dir does not exists just return if (!FolderExists(szFolder)) { return true; } if (DeleteFolderContents(szFolder, TRUE, NULL, hTerminate, bProcessMsgLoop)) { ::SetFileAttributes(szFolder, FILE_ATTRIBUTE_NORMAL); return (RemoveDirectory(szFolder) == TRUE); } return false; }
bool cFile::DeleteFolderContents(const AString & a_FolderName) { auto Contents = cFile::GetFolderContents(a_FolderName); for (const auto & item: Contents) { // Skip "." and ".." altogether: if ((item == ".") || (item == "..")) { continue; } // Remove the item: auto WholePath = a_FolderName + GetPathSeparator() + item; if (IsFolder(WholePath)) { if (!DeleteFolderContents(WholePath)) { return false; } if (!DeleteFolder(WholePath)) { return false; } } else { if (!DeleteFile(WholePath)) { return false; } } } // for item - Contents[] // All deletes succeeded return true; }
bool FileMisc::DeleteFolderContents(const TCHAR* szFolder, BOOL bIncludeSubFolders, const TCHAR* szFileMask, HANDLE hTerminate, BOOL bProcessMsgLoop) { // if the dir does not exists just return if (!FolderExists(szFolder)) { return true; } // if a file mask has been specified with subfolders we need to do 2 passes on each folder, // one for the files and one for the sub folders int nPasses = (bIncludeSubFolders && (szFileMask && lstrlen(szFileMask))) ? 2 : 1; bool bResult = true; bool bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0); for (int nPass = 0; !bStopped && nPass < nPasses; nPass++) { CString sSearchSpec(szFolder), sMask(szFileMask); if (sMask.IsEmpty() || nPass == 1) // (nPass == 1) == 2nd pass (for folders) { sMask = "*.*"; } TerminatePath(sSearchSpec); sSearchSpec += sMask; WIN32_FIND_DATA finfo; HANDLE hSearch = NULL; if ((hSearch = FindFirstFile(sSearchSpec, &finfo)) != INVALID_HANDLE_VALUE) { do { if (bProcessMsgLoop) { Misc::ProcessMsgLoop(); } if (finfo.cFileName[0] != '.') { CString sItem(szFolder); sItem += "\\"; sItem += finfo.cFileName; if (finfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (bIncludeSubFolders && (nPass == 1 || nPasses == 1)) { if (DeleteFolderContents(sItem, TRUE, szFileMask, hTerminate, bProcessMsgLoop)) { if (!szFileMask || !lstrlen(szFileMask)) { bResult = (RemoveDirectory(sItem) == TRUE); } } } } else { bResult = (DeleteFile(sItem) == TRUE); } } bStopped = (WaitForSingleObject(hTerminate, 0) == WAIT_OBJECT_0); } while (!bStopped && bResult && FindNextFile(hSearch, &finfo)); FindClose(hSearch); } } return (!bStopped && bResult); }