/** * Empty the directory i.e. delete all the files inside it * @param sDir Directory to empty */ void COfflineBrowser::EmptyDirectory(std::string sDir) { //Keep iterating through the directory if you find a directory recursively call itself and //if you wont find a file add it to the vector m_dirArray.push_back(sDir); WIN32_FIND_DATA findData; std::string sFile = sDir + "\\*"; HANDLE hFind = FindFirstFile(sFile.c_str(), &findData); if(hFind == INVALID_HANDLE_VALUE) return; do { if(strcmpi(findData.cFileName, ".") == 0 || strcmpi(findData.cFileName, "..") == 0) continue; std::string sDeleteFile = sDir + "\\" + findData.cFileName; if(findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) EmptyDirectory(sDeleteFile); else m_fileArray.push_back(sDeleteFile); }while(FindNextFile(hFind, &findData)); FindClose(hFind); }
BOOL CDustman::EmptyDirectory( LPCTSTR szPath, BOOL bDeleteDesktopIni /*= FALSE*/, BOOL bWipeIndexDat /*= FALSE*/ ) { WIN32_FIND_DATA wfd; HANDLE hFind; CString sFullPath; CString sFindFilter; DWORD dwAttributes = 0; sFindFilter = szPath; sFindFilter += _T("\\*.*"); if ((hFind = FindFirstFile(sFindFilter, &wfd)) == INVALID_HANDLE_VALUE) { return FALSE; } do { if (_tcscmp(wfd.cFileName, _T(".")) == 0 || _tcscmp(wfd.cFileName, _T("..")) == 0 || (bDeleteDesktopIni == FALSE && _tcsicmp(wfd.cFileName, _T("desktop.ini")) == 0)) { continue; } sFullPath = szPath; sFullPath += _T('\\'); sFullPath += wfd.cFileName; //去掉只读属性 dwAttributes = GetFileAttributes(sFullPath); if (dwAttributes & FILE_ATTRIBUTE_READONLY) { dwAttributes &= ~FILE_ATTRIBUTE_READONLY; SetFileAttributes(sFullPath, dwAttributes); } if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { EmptyDirectory(sFullPath, bDeleteDesktopIni, bWipeIndexDat); RemoveDirectory(sFullPath); } else { if (bWipeIndexDat && _tcsicmp(wfd.cFileName, _T("index.dat")) == 0) { WipeFile(szPath, wfd.cFileName); } DeleteFile(sFullPath); } } while (FindNextFile(hFind, &wfd)); FindClose(hFind); return TRUE; }
BOOL CDustman::ClearInternetTempFile() { // //清internet临时文件 TCHAR szPath[MAX_PATH]; DeleteUrlCache(File); if (SHGetSpecialFolderPath(NULL, szPath, CSIDL_INTERNET_CACHE, FALSE)) { //得到临时目录,并清空它. EmptyDirectory(szPath); } return 0; }
BOOL CDiskObject::EmptyDirectories( const CString& directory ) /* ============================================================ Function : CDiskObject::EmptyDirectories Description : Will delete all files in "directory". Will also empty subdirectories. Access : Public Return : BOOL - "TRUE" if OK. "GetErrorMessage" will get an error string if "FALSE" Parameters : CString directory - the root directory to empty. Usage : Call to empty a directory tree. ============================================================*/ { ClearError( ); CString indir( directory ); QualifyPath( indir ); CStringArray directories; // Get all directories BOOL result = EnumAllDirectories( indir, directories ); if( result ) { INT_PTR max = directories.GetSize( ); // Loop and empty for( INT_PTR t = max - 1 ; t >= 0 ; t-- ) if( !( result = EmptyDirectory( directories[ t ] ) ) ) t = -1; if( result ) result = EmptyDirectory( indir ); } return result; }
/** * Delete the directory * @param sDir Directory to delete */ void COfflineBrowser::DeleteDirectory(std::string sDir) { //Clean up previously filled arrays m_dirArray.erase(m_dirArray.begin(), m_dirArray.end()); m_fileArray.erase(m_fileArray.begin(), m_fileArray.end()); //Empty directory fist before creating EmptyDirectory(sDir); //Delete all the files for(int a=m_fileArray.size()-1;a>=0;a--) DeleteFile(m_fileArray[a].c_str()); //Delete every directory now which are now empty for(a=m_dirArray.size()-1;a>=0;a--) RemoveDirectory(m_dirArray[a].c_str()); }
BOOL CDiskObject::RemoveDirectory( const CString& directory ) /* ============================================================ Function : CDiskObject::RemoveDirectory Description : Will remove the directory "directory", even if not empty. Will not remove subdirectories. Access : Public Return : BOOL - "TRUE" if OK. "GetErrorMessage" will get an error string if "FALSE" Parameters : CString directory - directory to remove. Usage : Call to remove a directory. ============================================================*/ { ClearError( ); BOOL result = TRUE; CString indir( directory ); if( indir.GetLength( ) ) { QualifyPath( indir ); // Wipe and remove directory if( ( result = EmptyDirectory( indir ) ) ) { Trigger( indir ); if( !( result = ::RemoveDirectory( indir ) ) ) SetSystemErrorMessage( ::GetLastError( ), indir ); } } else { // Small sanity check, we can't // delete the current directory. SetInternalErrorMessage( ); result = FALSE; } return result; }
void FastOS_FileInterface::EmptyAndRemoveDirectory(const char *dir) { EmptyDirectory(dir); FastOS_File::RemoveDirectory(dir); }