/* 功能在文件夾中搜索后綴相同的文件 szFilePath 文件夾路徑 strExt 要搜索的文件后綴 */ BOOL FindIsFiles(CString szFilePath, CString strExt) { CFileFind finder; BOOL bWorking = FALSE;
// bWorking = PathIsRoot(szFilePath); // if ( PathIsRoot(szFilePath) )///判斷是否是盤符 // { // return FALSE; // } // bWorking = PathIsDirectory(szFilePath); // if (PathIsDirectory(szFilePath))//判斷是否是文件路徑 // { // }
if(szFilePath.GetLength()) bWorking = finder.FindFile(szFilePath + _T("http://*.*"));while(bWorking){ bWorking = finder.FindNextFile(); if (finder.IsDots()) { if((finder.GetFileName() == _T("..")) && (bWorking == FALSE)) break; continue; } if (finder.IsDirectory()) { CString str = finder.GetFilePath(); CString szFileName = finder.GetFileName(); CString strFileUp = szFileName, strExtUp = strExt; strFileUp.MakeUpper(); strExtUp.MakeUpper(); if(strFileUp.Right(strExtUp.GetLength()) == strExtUp) { return TRUE; } } else { CString str = finder.GetFilePath(); CString szFileName = finder.GetFileName(); CString strFileUp = szFileName, strExtUp = strExt; strFileUp.MakeUpper(); strExtUp.MakeUpper(); if(strFileUp.Right(strExtUp.GetLength()) == strExtUp) { return TRUE; } }}return FALSE;}
/* 重命名所有文件,還沒有實現完全 szFilePath:文件路徑 szFileName:重命名的文件 */ BOOL RenameAllFile(CString szFilePath, vector& szFileName) { CFileFind finder; BOOL bWorking = FALSE;
if(szFilePath.GetLength()) bWorking = finder.FindFile(szFilePath + _T("http://*.*"));while(bWorking){ bWorking = finder.FindNextFile(); if (finder.IsDots()) { if((finder.GetFileName() == _T("..")) && (bWorking == FALSE)) break; continue; } if (finder.IsDirectory()) { CString str = finder.GetFilePath(); RenameAllFile(str, szFileName); } else { szFileName.push_back(finder.GetFileName()); }}return TRUE;} /* 功能:從當前文件夾szFilePath 開始查找strExt,如果查找到則把它放入vctFind中 描述:針對車型升級失敗,而car.bmp又刪除不掉,這個時候必須對car.bmp進行重名, 否則在讀取車型時,會顯示這張圖片,但是點擊又不能啟動診斷程序。 對于其他文件,就不需要這樣處理了,只需要車型文件夾重命名就可以了 參數: szFilePath : 車型文件夾名稱 strExt : car.bmp vctFind : 存放需要重名的文件 */ BOOL RenameCarBmp(CString szFilePath, CString strExt, vector& vctFind) { CFileFind finder; BOOL bWorking = FALSE;
if(szFilePath.GetLength()) bWorking = finder.FindFile(szFilePath + _T("http://*.*"));while(bWorking){ bWorking = finder.FindNextFile(); if (finder.IsDots()) { if((finder.GetFileName() == _T("..")) && (bWorking == FALSE)) break; continue; } if (finder.IsDirectory()) { CString str = finder.GetFilePath(); RenameCarBmp(str, strExt, vctFind); } else { CString szFileName = finder.GetFileName(); CString strFileUp = szFileName, strExtUp = strExt; strFileUp.MakeUpper(); strExtUp.MakeUpper(); if(strFileUp.Right(strExtUp.GetLength()) == strExtUp) { vctFind.push_back(szFilePath+_T("http://")+szFileName); } }}return TRUE;}
//ReNameFolder //參數:lpszFromPath 源文件夾路徑,lpszToPath 目的文件夾路徑 //作用:修改原文件夾的名字 // BOOL ReNameFiles(CString& lpszFolderPath, CString& lpszFilePRev) { CString strFileName = _T(“”);
BOOL bRet;int idx = 0;if (GetFileAttributes(lpszFolderPath) == -1)///zp 20161025 說明文件不存在,可以接著升級{ m_FilePath.SetFocus(); AfxMessageBox(_T("文件路徑錯誤?。?!請重新指定")); return TRUE;}if (lpszFilePrev.GetLength() <= 0)///zp 20161025 說明文件不存在,可以接著升級{ m_FilePrev.SetFocus(); AfxMessageBox(_T("請指定文件前綴?。?!")); return TRUE;}vector<CString> vctGuoJia;vctGuoJia.push_back(_T("CN"));vctGuoJia.push_back(_T("DE"));vctGuoJia.push_back(_T("EN"));vctGuoJia.push_back(_T("ES"));vctGuoJia.push_back(_T("FR"));vctGuoJia.push_back(_T("HE"));vctGuoJia.push_back(_T("HK"));vctGuoJia.push_back(_T("HU"));vctGuoJia.push_back(_T("IT"));vctGuoJia.push_back(_T("JP"));vctGuoJia.push_back(_T("KR"));vctGuoJia.push_back(_T("NL"));vctGuoJia.push_back(_T("PL"));vctGuoJia.push_back(_T("PT"));vctGuoJia.push_back(_T("RU"));vctGuoJia.push_back(_T("SE"));vctGuoJia.push_back(_T("TH"));int i = 0;CString strCarName=_T("CAR.BMP");while (bRet){ strFileName + vctGuoJia.at(i++); bRet = FindIsFiles(lpszFolderPath, strCarName);///找CarName00到返回TRUE///沒有找到說明文件不存在返回FALSE if (!bRet) { continue; } idx++;}vector<CString> cstrFilePath;cstrFilePath.push_back(strCarName);if (PathIsDirectory(lpszFolderPath+_T("http://"))==FILE_ATTRIBUTE_DIRECTORY)///判斷輸入路徑是那種類型 1 文件夾 2 文件{ RenameCarBmp(lpszFolderPath, _T("car.bmp"), cstrFilePath); ///車型升級car.bmp這個地方出問題重命名圖片名稱}int nRet = -1;for (int i=cstrFilePath.size()-1; i >= 0 ; i--)///這個地方是重命名文件夾和{ CString NewPathFrm = _T(""); CString NewDstPathFrm = _T(""); if (0 == i)///只重命名文件夾 { NewPathFrm = lpszFolderPath; NewPathFrm +='/0';//注意必須是'/0'而不是"/0"!~!! NewDstPathFrm = lpszFolderPath+_T("http://")+cstrFilePath.at(i); lpszFolderPath = NewDstPathFrm; } else///文件夾和里面文件都進行重命名 { CString strExt = _T(""); NewPathFrm.Format(_T("0%d"), idx); strExt = cstrFilePath.at(i); strExt.Insert(strExt.ReverseFind('.'), NewPathFrm); if (strExt.Find(lpszFolderPath) == -1) { NewDstPathFrm = lpszFolderPath + _T("http://") + strExt; NewPathFrm = lpszFolderPath + _T("http://") +cstrFilePath.at(i); } else { NewDstPathFrm = strExt; NewPathFrm = cstrFilePath.at(i); } NewPathFrm +='/0';//注意必須是'/0'而不是"/0"!~!! } SHFILEOPSTRUCT FileOp; ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT)); FileOp.fFlags = FOF_NOCONFIRMATION ; FileOp.hNameMappings = NULL; FileOp.hwnd = NULL; FileOp.lpszProgressTitle = NULL; FileOp.pFrom = NewPathFrm; FileOp.pTo = NewDstPathFrm; FileOp.wFunc = FO_RENAME; nRet = SHFileOperation(&FileOp);}return (nRet == 0);}
新聞熱點
疑難解答