文件读写方法小记2
来源:互联网 发布:java ee源码下载 编辑:程序博客网 时间:2024/05/23 16:04
①遍历文件夹并且计算文件夹个数
int iCount = 0; CString strTempPath; CString strFileName; strTempPath = CBApp::getCurrModulePath() + _T("ini"); BOOL bWorking = finder.FindFile(strTempPath + _T("\\*.*")); while (bWorking) { bWorking = finder.FindNextFile(); if(!finder.IsDots()) { iCount++; } strFileName = finder.GetFilePath(); }
②删除非空的文件夹
bool CBSetting::DeleteDirectory(CString sPath) { CFileFind Finder; CString sDir; sDir.Format(_T("%s\\*.*"),sPath); BOOL IsFinded = Finder.FindFile(sDir); while (IsFinded) { IsFinded = Finder.FindNextFile(); if (!Finder.IsDots()) { if (Finder.IsDirectory()) DeleteDirectory(Finder.GetFilePath()); else DeleteFile(Finder.GetFilePath()); } } Finder.Close(); if(!RemoveDirectory(sPath)) { return FALSE; } return TRUE; }
③文件的删除和重命名
//删除预设文件void CBSetting::deleteSettingFile(CString &sBoyeApp, CBObList &settingList, int iIndex){ CFileFind Finder; CString sName; sName.Format(_T("\\%d.ini"), iIndex); CString strPath = sBoyeApp + sName; if(Finder.FindFile(strPath)) { DeleteFile(strPath); } else return; CString sOldName, sOldPath; for (POSITION pos = settingList.FindIndex(iIndex); pos != NULL; iIndex ++) { settingList.GetNext(pos); sOldName.Format(_T("\\%d.ini"), iIndex + 1); sOldPath = sBoyeApp + sOldName; if(Finder.FindFile(sOldPath)) CFile::Rename(sOldPath, strPath); //重命名文件 strPath = sOldPath; }}
④文件夹的删除和重命名
//删除对应的设备保存文件void CBSetting::deleteDeviceFile(int iIndex){ CString sPath; CString sOldPath; CFileFind Finder; sPath.Format(_T("ini\\%d"), iIndex); sPath = CBApp::getCurrModulePath() + sPath; if(Finder.FindFile(sPath)) { DeleteFolder(sPath); } else return; for (POSITION pos = m_DeviceList.FindIndex(iIndex); pos != NULL; iIndex ++) { m_DeviceList.GetNext(pos); sOldPath.Format(_T("ini\\%d"), iIndex + 1); sOldPath = CBApp::getCurrModulePath() + sOldPath; if(Finder.FindFile(sOldPath)) ReNameFolder(sOldPath, sPath); sPath = sOldPath; } Finder.Close();}//删除文件夹void CBSetting::DeleteFolder(CString sOldPath) { int nLength = sOldPath.GetLength(); TCHAR *pNewPath = new TCHAR[nLength+2]; memcpy(pNewPath, sOldPath.GetBuffer(), nLength * sizeof(TCHAR)); pNewPath[nLength] = '\0'; pNewPath[nLength+1] = '\0'; SHFILEOPSTRUCT FileOp; ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT)); FileOp.fFlags = FOF_NOCONFIRMATION; FileOp.hNameMappings = NULL; FileOp.hwnd = NULL; FileOp.lpszProgressTitle = NULL; FileOp.pFrom = pNewPath; FileOp.pTo = NULL; FileOp.wFunc = FO_DELETE; SHFileOperation(&FileOp); delete pNewPath;}//重命名文件夹void CBSetting::ReNameFolder(CString sOldName,CString sNewName){ int nLength = sOldName.GetLength(); TCHAR *pOldPathName = new TCHAR[nLength + 2]; memcpy(pOldPathName, sOldName.GetBuffer(), nLength * sizeof(TCHAR)); pOldPathName[nLength] = '\0'; pOldPathName[nLength + 1] = '\0'; SHFILEOPSTRUCT FileOp; ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT)); FileOp.fFlags = FOF_NOCONFIRMATION ; FileOp.hNameMappings = NULL; FileOp.hwnd = NULL; FileOp.lpszProgressTitle = NULL; FileOp.pFrom = pOldPathName; FileOp.pTo = sNewName; FileOp.wFunc = FO_RENAME; SHFileOperation(&FileOp); delete pOldPathName;}