C++ 删除一个文件目录下的所有文件以及目录
1 //删除一个文件目录下的所有文件,并删除当前文件夹 chrDirName = _T("c:\\123"); 2 BOOL CIVMSvrInfo::ReleaseDirectory(CString chrDirName) 3 { 4 BOOL bRemove = DoRemoveDirectory(chrDirName); 5 if (bRemove) 6 ::RemoveDirectory(chrDirName); 7 else 8 return FALSE; 9 10 return TRUE; 11 12 } //end of CIVMSvrInfo::ReleaseDirectory() 13 14 //删除一个文件目录结构下的所有文件(不删除当前文件夹) 15 BOOL CIVMSvrInfo::DoRemoveDirectory(CString chrDirName) 16 { 17 CString strDirName = chrDirName; 18 19 BOOL result; 20 HANDLE Handle; 21 WIN32_FIND_DATA fData; 22 CString strTemp; 23 24 Handle = FindFirstFile(strDirName + "\\*.*", &fData); 25 26 if (Handle == INVALID_HANDLE_VALUE) 27 return FALSE; 28 29 do { 30 if ( fData.cFileName[0] == ‘.‘ ) 31 continue; 32 33 if (fData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { 34 if (strDirName[strDirName.GetLength()-1] != ‘\\‘){ 35 TCHAR chA[MAX_PATH]; 36 CString strA = strDirName+‘\\‘+ fData.cFileName; 37 lstrcpy(chA, strA); 38 DoRemoveDirectory(chA); 39 } 40 else{ 41 TCHAR chB[MAX_PATH]; 42 CString strB = strDirName + fData.cFileName; 43 lstrcpy(chB, strB); 44 DoRemoveDirectory(chB); 45 } 46 47 strTemp = strDirName + "\\" + fData.cFileName; 48 49 SetFileAttributes(strTemp, ~FILE_ATTRIBUTE_READONLY); 50 if (!RemoveDirectory(strTemp)) 51 result = FALSE; 52 else 53 result = TRUE; 54 } 55 else 56 { 57 strTemp = strDirName + "\\" + fData.cFileName; 58 BOOL bl = SetFileAttributes(strTemp, ~FILE_ATTRIBUTE_READONLY); 59 60 if (!DeleteFile(strTemp)) 61 result = FALSE; 62 else 63 result = TRUE; 64 } 65 66 }while(FindNextFile(Handle,&fData)); 67 68 if (Handle) 69 FindClose(Handle); 70 71 return result; 72 73 } // end of BOOL PreRemoveDirectory(CString DirName)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。