// // 機能 : 古いログの削除 // // 機能説明 : LOG_EXPIRE 日以上前のログは削除 // // 返り値 : true 正常終了 // false エラー発生 // // 備考 : // bool CTPerror::DelOldLog() { CString str; CFileFind finder; CTime ntm; CTime tm; CTimeSpan tms; CFileSpec fs; /// LOG_EXPIRE期間以前のログファイルを削除 if (!SetCurrentDirectory(LogDir)) return false; ntm = CTime::GetCurrentTime(); BOOL bWorking = finder.FindFile(_T("*.log")); if (!bWorking) return false; str.Empty(); while (bWorking) { bWorking = finder.FindNextFile(); if (!finder.IsDirectory()) { str = finder.GetFileName(); /// ログファイル作成日付と現在との差分 finder.GetCreationTime(tm); tms = ntm - tm; if (tms.GetDays() > LOG_EXPIRE) { fs.SetFullSpec(str); if (!fs.Exist()) continue; fs.FileDelete(); } } } finder.Close(); return true; }
/******************************************************** getLatestLogFileName -- find file with right extension and latest *creation* time stamp // Last written timestamp not good enough, since depending on OS(!) and handle handling, write timestamp may not be updated until closing of file... ********************************************************/ CString getLatestLogFileName(CString logPath,CString fileExtension, CString SpecificFile) { CString fileName; CFileFind finder; CString candidate; CTime candidateCreationTime,fileNameCreationTime; char singleChar; //Check if a specific file is configured.If so, no work to be done if (SpecificFile!="") { return SpecificFile; } // build a string with wildcards CString strWildcard(logPath); strWildcard += _T("\\*"); if (fileExtension!="") { singleChar=fileExtension.GetAt(0); if (singleChar!='.') { strWildcard += _T("."); } strWildcard += _T(fileExtension); } // start searching for files BOOL bWorking = finder.FindFile(strWildcard); while (bWorking) { bWorking = finder.FindNextFile(); //must be called according to spec before finding any file.. // skip . and .. files; otherwise, we'd recur infinitely! if (finder.IsDots()) continue; //Get file info candidate=finder.GetFileName(); finder.GetCreationTime(candidateCreationTime); if ((fileName=="")||(candidateCreationTime>fileNameCreationTime)) { fileName=candidate; fileNameCreationTime=candidateCreationTime; } } if (fileName=="") return fileName; //If no valid file at all. fileName=logPath + "\\" + fileName; return fileName; }
//把录像文件按时间逆序,然后反应到控件上 yjj 090304 void CAppealDlg::ProcessRecordFile(const CString& strUserName) { if (strUserName == "") { return; } GetDlgItem(IDC_EDIT_USERNAME)->SetWindowText(strUserName); GetDlgItem(IDC_EDIT_PHONE_NUM)->SetWindowText(""); GetDlgItem(IDC_EDIT_APPEAL_EMAIL)->SetWindowText(""); GetDlgItem(IDC_EDIT_APPEAL_CONTENT)->SetWindowText(""); CString strPath = CBcfFile::GetAppPath(); //得到当前的目录 strPath += "log"; strPath += "\\"; CString gamenamefile = strPath + "gamename.bcf"; DWORD dwHandle = cfgOpenFile(gamenamefile); if(dwHandle < 0x10) return; strPath += "log_"; strPath += strUserName; strPath += "\\"; CTime tCurTime = CTime::GetCurrentTime(); CString direct = tCurTime.Format("%Y-%m-%d"); CString strCurDir = strPath + direct; strCurDir += "\\"; CFileFind finder; strCurDir += _T("*.*"); int iFindFileCount = 0; m_fileList.clear(); //从当前,向前找6个目录 for (int i=0; i<6; i++) { BOOL bWorking = finder.FindFile(strCurDir); while (bWorking) { bWorking = finder.FindNextFile(); if (finder.IsDots()) continue; //找到一个文件 //CString sFileName = finder.GetFileName(); //CString sFilePath = finder.GetFilePath(); RecordFileStruct recordfile; recordfile.strWholeName = finder.GetFilePath(); //得到完整名字 recordfile.strFileName = finder.GetFileName(); //得到文件名字 //wushuqun 2009.5.20 recordfile.strGamePath = finder.GetFilePath(); //if (recordfile.strFileName.Find(".zxh") == -1) //{ // continue; //} CString strNameId = GetFileNameID(recordfile.strFileName); recordfile.strGameName = GetGameName(dwHandle,strNameId); //得到游戏名字 if (recordfile.strGameName == "") { continue; } finder.GetCreationTime(recordfile.timeCreatTime); //得到创建文件时间 recordfile.strGameTime = recordfile.timeCreatTime.Format("%m-%d %H:%M "); m_fileList.push_back(recordfile); //把文件信息加入链表 iFindFileCount ++; } //找完整个目录 if (iFindFileCount >= 20) { break; } // CTimeSpan ts(1, 0, 0, 0); tCurTime -= ts; direct = tCurTime.Format("%Y-%m-%d"); strCurDir = strPath + direct; strCurDir += "\\"; strCurDir +=_T("*.*"); //找上一天的目录 } //按升序排列 m_fileList.sort(); m_FileListCtrl.DeleteAllItems(); list<RecordFileStruct>::iterator iter = m_fileList.begin(); int iCount = 0; for (; iter != m_fileList.end(); iter++) { RecordFileStruct recordfile = *iter; //RecordFileStruct* pRecordFile = iter; //iter ++; //CString strListNmae; //strListNmae.Format("%s %s",recordfile.strGameName,recordfile.strGameTime); int iItem = m_FileListCtrl.InsertItem(m_FileListCtrl.GetItemCount(),recordfile.strGameName); m_FileListCtrl.SetItemText(iItem,1,recordfile.strGameTime); //wushuqun 2009.5.20 //新增加一列“文件名称" m_FileListCtrl.SetItemText(iItem,2,recordfile.strFileName); //m_FileListCtrl.SetItemData(iItem,(DWORD)&iter); iCount++; //m_ListBox.AddString(strListNmae); //m_ListBox.InsertString(iCount++,strListNmae); if (iCount == 20) { break; } } cfgClose(dwHandle); finder.Close(); }
//------------------------------------------------ // 파일의 리스트 및 각 파일에 대한 자세한 정보를 // 함께 저장하게 됨 // data.h파일에 해당 구조체를 선언한다. //-------------------------------------------------- void CMyExplorerDoc::SelectTreeViewFolder(CString strFullName) { LIST_VIEW* pListView; CFileFind ff; // 사용자가 폴더를 선택할 때마다 파일 리스트를 // 새로 업데이트 해야 함 // 따라서 기존 정보를 모두 삭제한다. if (m_pFileList != NULL) RemoveAllFileList(); m_pFileList = new CObList; SetCurrentPath(strFullName); strFullName += "*.*"; if (ff.FindFile(strFullName) == TRUE) { BOOL bFlag = TRUE; while(bFlag == TRUE) { bFlag = ff.FindNextFile(); // 디렉토리 , 도트파일이면 다시 찾음 if (ff.IsDirectory() || ff.IsDots()) continue; // 파일 정보를 알아내서LIST_VIEW 구조체에 // 저장한 후 // 그것을 모두 m_pFileList에 저장한다. pListView = new LIST_VIEW; InitListViewStruct(pListView); pListView->strName = ff.GetFileName(); pListView->strPath = ff.GetFilePath(); CString strName = pListView->strName; CString strExt = ff.GetFileTitle(); int nNum = strName.GetLength() - strExt.GetLength(); if (nNum == 0) strExt = ""; else strExt = strName.Right(nNum - 1); pListView->strKind = strExt + " 파일"; pListView->dwFileSize = ff.GetLength(); CTime time; if (ff.GetCreationTime(time) == TRUE) pListView->tCreateTime = time; if (ff.GetLastAccessTime(time) == TRUE) pListView->tLastAccessTime = time; if (ff.GetLastWriteTime(time) == TRUE) pListView->tLastWriteTime = time; if (ff.IsHidden() == TRUE) pListView->bIsHidden = TRUE; if (ff.IsReadOnly() == TRUE) pListView->bIsReadOnly = TRUE; if (ff.IsArchived() == TRUE) pListView->bIsArchived = TRUE; if (ff.IsSystem() == TRUE) pListView->bIsSystem = TRUE; m_pFileList->AddTail((CObject*)pListView); } } ff.Close(); //------------------------------ m_pExpListView->SetFileList(); //------------------------------------- }
DWORD CCpDialog::UpdateFolder(LPCSTR strSrcFolder, LPCSTR strDstFolder, bool bIncludeSubFolders) { CString strSrcPath = strSrcFolder + CString("*.*"); DWORD dwFileCount = 0; CFileFind FileFind; BOOL bFound = FileFind.FindFile(strSrcPath); while (bFound) { bFound = FileFind.FindNextFile(); if (FileFind.IsDots()) continue; CString strSrcFilePath = FileFind.GetFilePath(); CString strSrcFileName = FileFind.GetFileName(); if (FileFind.IsDirectory()) { if (!bIncludeSubFolders) continue; CString strNewDstFolder = CString(strDstFolder) + strSrcFileName + "\\"; bool bCreated = !!::CreateDirectory(strNewDstFolder, NULL); if (bCreated) dwFileCount++; CString strNewSrcFolder = strSrcFilePath + "\\"; dwFileCount += UpdateFolder(strNewSrcFolder, strNewDstFolder); continue; } FILETIME SrcCreationTime; FILETIME SrcAccessedTime; FILETIME SrcModfiedTime; FileFind.GetCreationTime(&SrcCreationTime); FileFind.GetLastAccessTime(&SrcAccessedTime); FileFind.GetLastWriteTime(&SrcModfiedTime); CString strDstFilePath = CString(strDstFolder) + strSrcFileName; if (FileExists(strDstFilePath)) { FILETIME DstCreationTime; FILETIME DstAccessedTime; FILETIME DstModfiedTime; bool bOK = GetFileTimes(strDstFilePath, DstCreationTime, DstAccessedTime, DstModfiedTime); if (CFileTime(SrcModfiedTime) <= CFileTime(DstModfiedTime)) continue; DWORD dwFileAttributes = ::GetFileAttributes(strDstFilePath); if (dwFileAttributes && FILE_ATTRIBUTE_READONLY) ::SetFileAttributes(strDstFilePath, dwFileAttributes & ~FILE_ATTRIBUTE_READONLY); } bool bCopied = !!::CopyFile(strSrcFilePath, strDstFilePath, false/*bFailIfExists*/); if (bCopied) { dwFileCount++; SetFileTimes(strDstFilePath, SrcCreationTime, SrcAccessedTime, SrcModfiedTime); } } FileFind.Close(); return dwFileCount; }
gbool GUrlCache::GetCacheStats(const char *directory, time_t &oldest,DWORD &spaceInUse, int &filesInUse) { CFileFind finder; CString dir = directory; // add separator int l = dir.GetLength(); if (l == 0) return FALSE; if ( !((dir[l-1] == '\\') || (dir[l-1] == '/'))) dir += '\\'; dir += "*.*"; CString path; CTime creationTime((time_t)0); CTime accessTime((time_t)0); CTime writeTime((time_t)0); // setup the find structure BOOL bWorking = finder.FindFile(dir); while (bWorking) { // for all entrys if (stop) break; bWorking = finder.FindNextFile(); path = finder.GetFilePath(); creationTime = (time_t)0; accessTime = (time_t)0; writeTime = (time_t)0; BOOL ret=finder.GetCreationTime(creationTime); finder.GetLastAccessTime(accessTime); finder.GetLastWriteTime(writeTime); time_t t = creationTime.GetTime(); if (accessTime.GetTime()>0) t = max(t,accessTime.GetTime()); if (finder.IsDots( )) { // ignore . .. } else if (finder.IsDirectory( )) { // recursively step down GetCacheStats(path,oldest,spaceInUse,filesInUse); } else { DWORD length = finder.GetLength(); TRACE("F %s c %ld a %ld w %ld size %ld \n",(const char *) path, creationTime.GetTime(),accessTime.GetTime(),writeTime.GetTime(),length); oldest = min(oldest,t); filesInUse++; spaceInUse += length; } } finder.Close(); return TRUE; }
// recursively remove a file branch gbool GUrlCache::RemoveFiles(const char *directory,time_t olderThan) { CFileFind finder; CString dir = directory; // add separator int l = dir.GetLength(); if (l == 0) return FALSE; if ( !((dir[l-1] == '\\') || (dir[l-1] == '/'))) dir += '\\'; dir += "*.*"; CString path; CTime creationTime((time_t)0); CTime accessTime((time_t)0); CTime writeTime((time_t)0); // setup the find structure BOOL bWorking = finder.FindFile(dir); LONGLONG fileSum=0; while (bWorking) { // for all entrys if (stop) break; bWorking = finder.FindNextFile(); path = finder.GetFilePath(); creationTime = (time_t)0; accessTime = (time_t)0; writeTime = (time_t)0; BOOL ret=finder.GetCreationTime(creationTime); finder.GetLastAccessTime(accessTime); finder.GetLastWriteTime(writeTime); time_t t = creationTime.GetTime(); // if (accessTime.GetTime()>0) t = max(t,accessTime.GetTime()); if (finder.IsDots( )) { // ignore . .. } else if (finder.IsDirectory( )) { // recursively step down RemoveFiles(path,olderThan); RemoveDirectory(path); } else { DWORD length = finder.GetLength(); TRACE("F %s c %ld a %ld w %ld size %ld \n",(const char *) path, creationTime.GetTime(),accessTime.GetTime(),writeTime.GetTime(),length); if (olderThan >0 ) { if (t < olderThan) { if (RemoveFile(path)) fileSum += length; } } else { if (RemoveFile(path)) fileSum += length; } } } finder.Close(); TRACE("%ld bytes deleted \n",(long) fileSum); return TRUE; }
// add files to list gbool GFileSorter::AddFiles(const char *directory,time_t &maxFileTime,BOOL &stop) { CFileFind finder; CString dir = directory; // add separator int l = dir.GetLength(); if (l == 0) return FALSE; if ( !((dir[l-1] == '\\') || (dir[l-1] == '/'))) dir += '\\'; dir += "*.*"; CString path; CTime creationTime((time_t)0); CTime accessTime((time_t)0); CTime writeTime((time_t)0); // setup the find structure BOOL bWorking = finder.FindFile(dir); while (bWorking) { // for all entrys if (stop) break; bWorking = finder.FindNextFile(); path = finder.GetFilePath(); creationTime = (time_t)0; accessTime = (time_t)0; writeTime = (time_t)0; BOOL ret=finder.GetCreationTime(creationTime); finder.GetLastAccessTime(accessTime); finder.GetLastWriteTime(writeTime); time_t t = creationTime.GetTime(); if (writeTime.GetTime()>0) t = max(t,writeTime.GetTime()); // HG wg Kristof if (accessTime.GetTime()>0) t = max(t,accessTime.GetTime()); if (finder.IsDots( )) { // ignore . .. } else if (finder.IsDirectory( )) { // recursively step down t=0; // we want to delete empty directories new 20.10.98 AddFiles(path,t,stop); DWORD length = 0; // to do get date of latest TRACE("D %s c %ld a %ld w %ld size %ld \n",(const char *) path, creationTime.GetTime(),accessTime.GetTime(),writeTime.GetTime(),length); // time is the max of the child time +1 GFSortEntry *e = new GFSortEntry(path,t+1,length,gtrue); if (!e) break; if (!Add(e)) break; if (t>maxFileTime) maxFileTime = t; } else { DWORD length = finder.GetLength(); // get length 64 fileSum += length; TRACE("F %s c %ld a %ld w %ld size %ld \n",(const char *) path, creationTime.GetTime(),accessTime.GetTime(),writeTime.GetTime(),length); GFSortEntry *e = new GFSortEntry(path,t,length); if (t>maxFileTime) maxFileTime = t; if (!e) break; if (!Add(e)) break; } } finder.Close(); //TRACE("%ld bytes \n",(long)fileSum); return TRUE; }