// FileOperTools.cpp: implementation of the CFileOperTools class. // ////////////////////////////////////////////////////////////////////// #include "geomative.h" #include "FileOperTools.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// extern SYSTEMTIME g_sysCurTime; extern int g_iUILanguage; CFileOperTools* CFileOperTools::m_pFileOper= NULL; CFileOperTools::CFileOperTools() { m_pComLog = NULL; m_pWriteLogSection = NULL; if (NULL == m_pWriteLogSection) { m_pWriteLogSection = new CRITICAL_SECTION; if (NULL == m_pWriteLogSection) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("分配日志的临界区失败!")); else MessageBoxEx(NULL, _T("Allocate critical section of log failed!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return ; } InitializeCriticalSection(m_pWriteLogSection); } } CFileOperTools::~CFileOperTools() { CloseComLog(); } CFileOperTools* CFileOperTools::GetInstance() { if (NULL == m_pFileOper) { m_pFileOper = new CFileOperTools(); } return m_pFileOper; } //strSrcPath:代表源目录,strDstPath代表目的目录 //在这里需要注意的是,strDstPath需要将源目录的文件名也包含进来 //比如想将c:\test目录拷贝到d盘下,那么此时strDstPath的值应该为d:\test bool CFileOperTools::CopyFolder(CString strSrcPath, CString strDstPath) { CString strError = _T(""); if (strDstPath.IsEmpty() || strDstPath.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("src_path和dst_path都不能为空!")); else MessageBoxEx(NULL, _T("Neither src_path nor dst_path can be empty!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } if (strSrcPath.Right(1) == _T("\\")) { strSrcPath = strSrcPath.Left(strSrcPath.GetLength() - 1); } if (strDstPath.Right(1) == _T("\\")) { strDstPath = strDstPath.Left(strDstPath.GetLength() - 1); } CreateDirectory(strDstPath,NULL); CFileFind finder; // 打开指定的文件夹进行搜索 BOOL bWorking = finder.FindFile(strSrcPath + _T("\\") + _T("*.*")); while(bWorking) { // 从当前目录搜索文件 bWorking = finder.FindNextFile(); CString strFileName = finder.GetFileName(); CString strSrc = strSrcPath + _T("\\") + strFileName; CString strDst = strDstPath + _T("\\") + strFileName; // 判断搜索到的是不是"."和".."目录 if(!finder.IsDots()) { // 判断搜索到的目录是否是文件夹 if(finder.IsDirectory()) { // 如果是文件夹的话,进行递归 if(!CopyFolder(strSrc, strDst)) { return false; } } else { // 如果是文件,进行复制 if(!CopyFile(strSrc, strDst, FALSE)) { DWORD dwError = GetLastError(); strError.Empty(); if (LANG_ZHCN == g_iUILanguage) { strError.Format(_T("复制文件夹失败,src_path = %s, dst_path = %s, errorno = %d"), strSrc, strDst, dwError); AfxMessageBox(strError); } else { strError.Format(_T("Copy folder failed,src_path = %s, dst_path = %s, errorno = %d"), strSrc, strDst, dwError); MessageBoxEx(NULL, strError, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return false; } } } } return true; } bool CFileOperTools::IsFileExist(CString strFileInfo) { return (INVALID_FILE_ATTRIBUTES == GetFileAttributes(strFileInfo)) ? false : true; } void CFileOperTools::CloseComLog() { if (NULL != m_pComLog) { fclose(m_pComLog); m_pComLog = NULL; } if (NULL != m_pWriteLogSection) { DeleteCriticalSection(m_pWriteLogSection); delete m_pWriteLogSection; m_pWriteLogSection = NULL; } } bool CFileOperTools::WriteComLog(unsigned char *pszData, int iDateLen) { if (NULL == m_pWriteLogSection) { return false; } EnterCriticalSection(m_pWriteLogSection); if (NULL == m_pComLog) { m_pComLog = fopen(m_strGeneralLogName, "ab+"); if (NULL == m_pComLog) { LeaveCriticalSection(m_pWriteLogSection); DWORD dwLastError = GetLastError(); CString strErr = _T(""); if (LANG_ZHCN == g_iUILanguage) { strErr.Format(_T("打开%s失败!错误码(%d)"), m_strGeneralLogName, dwLastError); AfxMessageBox(strErr); } else { strErr.Format(_T("Open %s failed!Errorcode(%d)"), m_strGeneralLogName, dwLastError); MessageBoxEx(NULL, strErr, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return false; } } char chTemp[100]; sprintf_s(chTemp, sizeof(chTemp), "threadid = %d,%04d-%02d-%02d %02d:%02d:%02d.%03d ",GetCurrentThreadId(), g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay, g_sysCurTime.wHour, g_sysCurTime.wMinute, g_sysCurTime.wSecond, g_sysCurTime.wMilliseconds); fwrite(chTemp, 1, strlen(chTemp), m_pComLog); fwrite(pszData, 1, iDateLen, m_pComLog); strcpy(chTemp,"\r\n"); fwrite(chTemp, 1, strlen(chTemp), m_pComLog); fflush(m_pComLog); LeaveCriticalSection(m_pWriteLogSection); return true; } bool CFileOperTools::WriteComLog(const CString& strInfo) { if (NULL == m_pWriteLogSection) { return false; } EnterCriticalSection(m_pWriteLogSection); if (NULL == m_pComLog) { m_pComLog = fopen(m_strGeneralLogName, "ab+"); if (NULL == m_pComLog) { LeaveCriticalSection(m_pWriteLogSection); DWORD dwLastError = GetLastError(); CString strErr = _T(""); if (LANG_ZHCN == g_iUILanguage) { strErr.Format(_T("打开%s失败!错误码(%d)"), m_strGeneralLogName, dwLastError); AfxMessageBox(strErr); } else { strErr.Format(_T("Open %s failed!Errorcode(%d)"), m_strGeneralLogName, dwLastError); MessageBoxEx(NULL, strErr, STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); } return false; } } CString strOutPut = _T(""); strOutPut.Format(_T("threadid = %d,%04d-%02d-%02d %02d:%02d:%02d.%03d %s \r\n"), GetCurrentThreadId(), g_sysCurTime.wYear, g_sysCurTime.wMonth, g_sysCurTime.wDay, g_sysCurTime.wHour, g_sysCurTime.wMinute, g_sysCurTime.wSecond, g_sysCurTime.wMilliseconds, strInfo); fwrite(strOutPut.GetBuffer(0), 1, strOutPut.GetLength(), m_pComLog); fflush(m_pComLog); LeaveCriticalSection(m_pWriteLogSection); return true; } bool CFileOperTools::DeleteFileDirect(CString strFilePath) { if (strFilePath.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("删除文件名不能为空!")); else MessageBoxEx(NULL, _T("Input filename can not be empty in deletefile!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } if (IsFileExist(strFilePath)) { if (!DeleteFile(strFilePath)) { CString strInfo = _T(""); strInfo.Format(_T("[%d]delete file failed!file_path = %s,error_code=%d"),__LINE__,strFilePath,GetLastError()); WriteComLog(strInfo); return false; } } return true; } bool CFileOperTools::DeleteDirectory(CString strDirPath) { if (strDirPath.IsEmpty()) { if (LANG_ZHCN == g_iUILanguage) AfxMessageBox(_T("输入参数错误!")); else MessageBoxEx(NULL, _T("Input paramter error in DeleteDirectory!"), STRING_MESSAGEBOXEX_TITLE, MB_OK, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US)); return false; } if (strDirPath.Right(1) == _T("\\")) { strDirPath = strDirPath.Left(strDirPath.GetLength() - 1); } if (!IsFileExist(strDirPath)) { return true; } CString strDelDirParh = _T(""); CFileFind tempFind; strDelDirParh.Format(_T("%s\\*.*"),strDirPath); BOOL IsFinded = tempFind.FindFile(strDelDirParh); CString strInfo = _T(""); while (IsFinded) { IsFinded = tempFind.FindNextFile(); if (!tempFind.IsDots()) { CString strFileName = tempFind.GetFileName(); CString strNeedDelete = strDirPath + _T("\\") + strFileName; if (tempFind.IsDirectory()) { if (!DeleteDirectory(strNeedDelete)) { tempFind.Close(); strInfo.Empty(); strInfo.Format(_T("[%d]delete directory failed!path = %s,error_code = %d"),__LINE__,strNeedDelete, GetLastError()); WriteComLog(strInfo); return false; } } else { if (!DeleteFile(strNeedDelete)) { tempFind.Close(); strInfo.Empty(); strInfo.Format(_T("[%d]delete file failed!path = %s,error_code = %d"),__LINE__,strNeedDelete, GetLastError()); WriteComLog(strInfo); return false; } } } } tempFind.Close(); if(!RemoveDirectory(strDirPath)) { strInfo.Empty(); strInfo.Format(_T("[%d]Remove directory failed!path = %s,error_code = %d"),__LINE__,strDirPath, GetLastError()); WriteComLog(strInfo); return false; } return true; } bool CFileOperTools::GeneralLogName() { SYSTEMTIME st; GetLocalTime(&st); m_strGeneralLogName.Format(_T("log\\general\\%04d%02d%02d.txt"), st.wYear, st.wMonth, st.wDay); return true; } bool CFileOperTools::DealGeneralLogFunc() { HANDLE hThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DealGeneralLogThread, this, 0, 0); if (INVALID_HANDLE_VALUE == hThread) { CFileOperTools::GetInstance()->WriteComLog(_T("Create Thread failed to simulate script run method")); return false; } CloseHandle(hThread); return true; } UINT CFileOperTools::DealGeneralLogThread(LPVOID lParam) { CFileOperTools* pThis = (CFileOperTools*)lParam; if (pThis == NULL) return false; //遍历general整个目录 //获取文件名,判断日期是否为两天前,满足删除 CString szCurFilePath = _T(""); szCurFilePath.Empty(); szCurFilePath.GetBufferSetLength(256); ::GetCurrentDirectory(szCurFilePath.GetLength(), szCurFilePath.GetBuffer(szCurFilePath.GetLength())); szCurFilePath.ReleaseBuffer(); CString strRecordDir; CString strRecordPath; strRecordPath.Format(_T("%s\\log\\general"), szCurFilePath); strRecordDir.Format(_T("%s\\*.*"), strRecordPath); WIN32_FIND_DATA wfd; HANDLE hFind = FindFirstFile(strRecordDir, &wfd); if (INVALID_HANDLE_VALUE == hFind) return false; CString strRecordFile = _T(""); CString strTmp; CString strFileExt = _T(""); CString strFileName; CString strTodayDate; SYSTEMTIME st; GetLocalTime(&st); strTodayDate.Format(_T("%04d%02d%02d"), st.wYear, st.wMonth, st.wDay); do { strRecordFile.Format(_T("%s\\%s"), strRecordPath, wfd.cFileName); strFileExt = PathFindExtension(strRecordFile); if ((wfd.dwFileAttributes&FILE_ATTRIBUTE_ARCHIVE) && (_tcscmp(strFileExt, _T(".txt")) == 0)) { strTmp.Format(_T("CFileOperTools::DeleteGeneralLog delete general log file:%s"), strRecordFile); CFileOperTools::GetInstance()->WriteComLog(strTmp); strFileName = wfd.cFileName; if (strFileName.Find('.') != -1) { strFileName = strFileName.Mid(0, strFileName.Find('.')); if (atoll(strTodayDate) - 1 > atoll(strFileName)) { pThis->DeleteFileDirect(strRecordFile); } } } } while (FindNextFile(hFind, &wfd)); FindClose(hFind); return true; } CString CFileOperTools::GetDstFilePathFolder() { LPITEMIDLIST rootLoation; BROWSEINFO bi; TCHAR targetPath[ MAX_PATH ]; SHGetSpecialFolderLocation( NULL, CSIDL_DESKTOP, &rootLoation ); ZeroMemory( &bi, sizeof( bi ) ); bi.pidlRoot = rootLoation; bi.lpszTitle = _T( "保存路径" ); LPITEMIDLIST targetLocation = SHBrowseForFolder( &bi ); if ( targetLocation == NULL ) return ""; SHGetPathFromIDList( targetLocation, targetPath ); CString szPath(targetPath); return szPath; }