plFileUtils::Modify plFileUtils::CompareModifyTimes(const char* file1, const char* file2) { plUnifiedTime modTime1, modTime2; if (GetFileTimes(file1, nil, &modTime1) && GetFileTimes(file2, nil, &modTime2)) { double diff = plUnifiedTime::GetTimeDifference(modTime1, modTime2); if (hsABS(diff) <= 2) return kFilesEqual; else if (diff > 0) return kFile1Newer; else return kFile2Newer; } return kFileError; }
//------------------------------------------------------------ //描述:2012.4.13王磊修改分隔符:改为. -改为_以方便应用到文件名中 bool CCommonFunc::GetFileTime(wchar_t* filePathStr, wchar_t* createTimeStr, int createTimeLen, wchar_t* accessTimeStr, int accessTimeen, wchar_t* writeTimeStr, int writeTimeLen, bool bHigh) { SYSTEMTIME tmCreate,tmAccess,tmWrite; if (!GetFileTimes(filePathStr, tmCreate, tmAccess, tmWrite)) { return false; } if (bHigh) { if (createTimeStr != 0) { SafeWStringPrintf(createTimeStr, createTimeLen, L"%d-%02d-%02d %02d:%02d:%02d:%02d", tmCreate.wYear, tmCreate.wMonth, tmCreate.wDay, tmCreate.wHour, tmCreate.wMinute, tmCreate.wSecond, tmCreate.wMilliseconds); } if (accessTimeStr!=0) { SafeWStringPrintf(accessTimeStr, accessTimeen, L"%d-%02d-%02d %02d:%02d:%02d:%02d", tmAccess.wYear, tmAccess.wMonth, tmAccess.wDay, tmAccess.wHour, tmAccess.wMinute, tmAccess.wSecond, tmAccess.wMilliseconds); } if (writeTimeStr != 0) { SafeWStringPrintf(writeTimeStr, writeTimeLen, L"%d-%02d-%02d %02d:%02d:%02d:%02d", tmWrite.wYear, tmWrite.wMonth, tmWrite.wDay, tmWrite.wHour, tmWrite.wMinute, tmWrite.wSecond, tmWrite.wMilliseconds); } } else { if (createTimeStr != 0) { SafeWStringPrintf(createTimeStr, createTimeLen, L"%d-%02d-%02d %02d:%02d:%02d", tmCreate.wYear, tmCreate.wMonth, tmCreate.wDay, tmCreate.wHour, tmCreate.wMinute, tmCreate.wSecond); } if (accessTimeStr != 0) { SafeWStringPrintf(accessTimeStr, accessTimeen, L"%d-%02d-%02d %02d:%02d:%02d", tmAccess.wYear, tmAccess.wMonth, tmAccess.wDay, tmAccess.wHour, tmAccess.wMinute, tmAccess.wSecond); } if (writeTimeStr != 0) { SafeWStringPrintf(writeTimeStr, writeTimeLen, L"%d-%02d-%02d %02d:%02d:%02d", tmWrite.wYear, tmWrite.wMonth, tmWrite.wDay, tmWrite.wHour, tmWrite.wMinute, tmWrite.wSecond); } } }
//----------------------------------------------------------------------------- bool Platform::getFileTimes(const char *filePath, FileTime *createTime, FileTime *modifyTime) { char pathName[MaxPath]; // if it starts with cwd, we need to strip that off so that we can look for // the file in the pref dir char cwd[MaxPath]; getcwd(cwd, MaxPath); if (dStrstr(filePath, cwd) == filePath) filePath = filePath + dStrlen(cwd) + 1; // if its relative, first look in the pref dir if (filePath[0] != '/' && filePath[0] != '\\') { MungePath(pathName, MaxPath, filePath, GetPrefDir()); if (GetFileTimes(pathName, createTime, modifyTime)) return true; } // here if the path is absolute or not in the pref dir MungePath(pathName, MaxPath, filePath, cwd); return GetFileTimes(pathName, createTime, modifyTime); }
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; }