// Parameter: const wchar_t * pFn1 输入文件迟冲仔名1
// Parameter: const wchar_t * pFn2 输入文件名2
//************************************
static const int iEarlier = -1 // 文件1比文件2创建晚
static const int iLater = 1 // 文件1比文件2创建早
static const int iEqual = 0// 创建时间相同(要注意的是,windows系统某些时候会延迟写文件,如果两个文件创建的间隔时间太短,
// 可能创建时间是一样的。此时文件码汪上右键属性时,看到的时间也是一样的判厅)
static const int iError = 2 //出错,无法判断
int CompareFileCreationTime(const wchar_t* pFn1, const wchar_t* pFn2)
{
if(NULL == pFn1 || NULL == pFn2)
{
return iError
}
if(0 == wcscmp(pFn1, pFn2))
{
return iEqual
}
//打开文件
HANDLE hFile1 = ::CreateFile(pFn1, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL)
HANDLE hFile2 = ::CreateFile(pFn2, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL)
if(INVALID_HANDLE_VALUE == hFile1 || INVALID_HANDLE_VALUE == hFile2)
{
if(INVALID_HANDLE_VALUE != hFile1)
{
::CloseHandle(hFile1)
hFile1 = NULL
}
if(INVALID_HANDLE_VALUE != hFile2)
{
::CloseHandle(hFile2)
hFile2 = NULL
}
return iError
}
//获取文件时间
FILETIME ftCreate1, ftCreate2, ft12, ft13, ft22, ft23
BOOL bSuc = ::GetFileTime(hFile1, &ftCreate1, &ft12, &ft13)
bSuc &= ::GetFileTime(hFile2, &ftCreate2, &ft22, &ft23)
::CloseHandle(hFile1)
::CloseHandle(hFile2)
if(FALSE == bSuc)
{
return iError
}
//先比较高位
if(ftCreate1.dwHighDateTime != ftCreate2.dwHighDateTime)
{
return (ftCreate1.dwHighDateTime >ftCreate2.dwHighDateTime ? iLater : iEarlier)
}
//再比较低位
if(ftCreate1.dwLowDateTime == ftCreate2.dwLowDateTime)
{
return iEqual
}
else
{
return (ftCreate1.dwLowDateTime >ftCreate2.dwLowDateTime ? iLater : iEarlier)
}
}
不清楚你的实际文件/情况,仅以问题中的说明及猜测为据;以下代码复制粘贴到记事本,另存为xx.bat,编码选ANSI
@echo off&cd /d "%~dp0"rem 用一个文件夹里的新文件缓悄替换掉另一个文件夹里的同名文件
set #=Any question&set _=WX&set $=Q&set/az=0x53b7e0b4
title %#% +%$%%$%/%_% %z%
set "folder1=D:\xxx\A"
set "folder2=D:\xxx\B"
if not exist 岩哪悔"%folder1%" (echo"%folder1%" 未找到&pause&exit)
if not exist "%folder2%" (echo"%folder2%" 未找到&pause&exit)
if "%folder1:~-1%" equ "\" set "folder1=%folder1:~,-1%"
if "%folder2:~-1%" 粗正equ "\" set "folder2=%folder2:~,-1%"
robocopy "%folder1%" "%folder2%" /E /R:3
echo%#% +%$%%$%/%_% %z%
pause
exit
#include <stdio.h>#include <stdlib.h>行烂
#include <time.h>
int days(char* begin_time) //格式: "20180914000000" //2018-9-14 0点0分0秒
{
struct tm tm1
time_t 枣郑time1,time_now
sscanf(begin_time,"%4d%2d%2d%2d%2d%2d",&tm1.tm_year,&tm1.tm_mon,&tm1.tm_mday,&tm1.tm_hour,&tm1.tm_min,&tm1.tm_sec)
tm1.tm_year-=1900
tm1.tm_mon--
tm1.tm_isdst=-1
time1=mktime(&tm1) //构档岩漏造time1
time_now=time(NULL)
tm1=*(localtime(&time_now)) //当时日期
return (time_now-time1)/(3600*24) //返回相差天数
}
int main()
{
char time_in_file[30]={"20180914000000\0"}
printf("\n%d",days(time_in_file))
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)