/*static*/ unsigned int CRhoFile::deleteFolder(const char* szFolderPath) { #if defined(WINDOWS_PLATFORM) && !defined(OS_WP8) StringW swPath; convertToStringW(szFolderPath, swPath); wchar_t* name = new wchar_t[ swPath.length() + 2]; swprintf(name, L"%s%c", swPath.c_str(), '\0'); translate_wchar(name, L'/', L'\\'); SHFILEOPSTRUCTW fop = {0}; fop.hwnd = NULL; fop.wFunc = FO_DELETE; fop.pFrom = name; fop.pTo = NULL; fop.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR #if defined(OS_WINDOWS_DESKTOP) || defined(OS_PLATFORM_MOTCE) | FOF_NOERRORUI #endif ; int result = SHFileOperationW(&fop); delete name; return result == 0 ? 0 : (unsigned int)-1; #elif defined(OS_WP8) StringW swPath; convertToStringW(szFolderPath, swPath); recursiveDeleteDirectory(swPath); return 0; #elif defined (OS_ANDROID) RemoveFileFunctor funct(szFolderPath); return funct(""); #else rho_file_impl_delete_folder(szFolderPath); return 0; #endif }
bool WindowsFiles::recursiveDeleteDirectory(const std::string& path) const { HANDLE hFind; WIN32_FIND_DATA FindFileData; TCHAR dirPath[MAX_PATH]; TCHAR fileName[MAX_PATH]; strcpy(dirPath,path.c_str()); strcat(dirPath, "\\*"); strcpy(fileName, path.c_str()); strcat(fileName,"\\"); // find the first file hFind = FindFirstFile(dirPath, &FindFileData); if(hFind == INVALID_HANDLE_VALUE) return false; strcpy(dirPath, fileName); bool bSearch = true; while(bSearch) { // until we find an entry if(FindNextFile(hFind,&FindFileData)) { if(isDots(FindFileData.cFileName)) continue; strcat(fileName,FindFileData.cFileName); if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { // we have found a directory, recurse if(!recursiveDeleteDirectory(fileName)) { FindClose(hFind); return false; // directory couldn't be deleted } // remove the empty directory RemoveDirectory(fileName); strcpy(fileName, dirPath); } else { if(!::DeleteFile(fileName)) { // some error occurred; close the handle and return FALSE FindClose(hFind); return false; } } } else { // no more files there if(GetLastError() == ERROR_NO_MORE_FILES) bSearch = false; else { // some error occurred; close the handle and return FALSE FindClose(hFind); return false; } } } FindClose(hFind); // close the file handle return RemoveDirectory(path.c_str()) == TRUE; // remove the empty directory }