您的位置:首页 > 其它

设置文件属性里的时间,包括:创建时间、访问时间、修改时间

2009-03-18 14:57 781 查看
// readFileCreateTime.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include<stdio.h>
#include<windows.h>

int _GetFileTime(char *szFileName);
int _SetFileTime(char *szFileName,char *szFileTime);
int StrToInt(char *str,int start,int end);
void helpMe(void);

int main(int argc,char *argv[])
{
if(argc!=2&&argc!=3)
helpMe();

else if(argc == 3)
{
if(strlen(argv[2])!=14)
{
printf("Parameter '%s' Error!Time string length is 14./n",argv[2]);
}
else
{
if(argc==3)_SetFileTime(argv[1],argv[2]);
}
}
else
{
if(argc==2)_GetFileTime(argv[1]);

}

return 0;
}

void helpMe(void)
{
printf("/t/t=================================================/n/n");
printf("/t/t/tGet&SetFileTime ver0.1 by Mxlong/n/n");
printf("/t/t+++++++++++++++++++++++++++++++++++++++++++++++++/n/n");
printf("/t/tUsage:/n");
printf("/t/t/tGetFileTime:GSTime FileName./n");
printf("/t/t/tExample:GSTime mxlong.txt/n/n");
printf("/t/t/tSetFileTime:GSTime FileName FileTime./n");
printf("/t/t/tExample:GSTime mxlong.txt 19820704213000/n/n");
printf("/t/t/t19820704213000=1982-07-04 21:30:00/n/n");
printf("/t/t+++++++++++++++++++++++++++++++++++++++++++++++++/n");
printf("/t/t/tQQ:289362563/t/tDate:2006.7.20/n");
printf("/t/t=================================================/n");
}

StrToInt(char *str,int start,int end)
{
int result=0;
if(start>end)
{
result=-1;
}
else
{
while(start<=end)
{
result=(str[start]-'0')+result*10;
start++;
}
}
return result;
}

int _GetFileTime(char *szFileName)
{
SYSTEMTIME st_systemTime;
FILETIME ft_localTime;
FILETIME lpCreationTime;
FILETIME lpLastAccessTime;
FILETIME lpLastWriteTime;
HANDLE hFile;
long retval;

hFile=CreateFile(szFileName,
GENERIC_READ,
NULL,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

if(hFile==INVALID_HANDLE_VALUE)
{
printf("Open File failed!/nErrorCode:%d/n",GetLastError());
CloseHandle(hFile);
return 1;
}

retval=GetFileTime(hFile,&lpCreationTime,&lpLastAccessTime,&lpLastWriteTime);

if(retval)
{
//CreationTime(创建时间)
FileTimeToLocalFileTime(&lpCreationTime,&ft_localTime);
FileTimeToSystemTime(&ft_localTime,&st_systemTime);

printf("CreationTime:%04d-%02d-%02d %02d:%02d:%02d/n",
st_systemTime.wYear ,
st_systemTime.wMonth ,
st_systemTime.wDay ,
st_systemTime.wHour ,
st_systemTime.wMinute ,
st_systemTime.wSecond);

//LastWriteTime(修改时间)
FileTimeToLocalFileTime(&lpLastWriteTime,&ft_localTime);
FileTimeToSystemTime(&ft_localTime,&st_systemTime);

printf("LastWriteTime:%04d-%02d-%02d %02d:%02d:%02d/n",
st_systemTime.wYear ,st_systemTime.wMonth ,st_systemTime.wDay ,
st_systemTime.wHour ,st_systemTime.wMinute ,st_systemTime.wSecond);

//LastAccessTime(访问时间)
FileTimeToLocalFileTime(&lpLastAccessTime,&ft_localTime);
FileTimeToSystemTime(&ft_localTime,&st_systemTime);

printf("LastAccessTime:%04d-%02d-%02d %02d:%02d:%02d/n",
st_systemTime.wYear ,st_systemTime.wMonth ,st_systemTime.wDay ,
st_systemTime.wHour ,st_systemTime.wMinute ,st_systemTime.wSecond);

CloseHandle(hFile);
return 0;
}
CloseHandle(hFile);
return 0;
}

int _SetFileTime(char *szFileName,char *szFileTime)
{
SYSTEMTIME st; //系统时间
FILETIME ft_localTime; //文件时间临时变量
FILETIME lpCreationTime; //创建时间
FILETIME lpLastAccessTime; //访问时间
FILETIME lpLastWriteTime; //修改时间
HANDLE hFile;
BOOL bResult;

hFile=CreateFile(szFileName,
GENERIC_WRITE,
NULL,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

if(hFile==INVALID_HANDLE_VALUE)
{
printf("Open File failed!/nErrorCode:%d/n",GetLastError());
CloseHandle(hFile);
return 1;
}

GetLocalTime(&st);
printf("CurrentTime:%04d-%02d-%02d %02d:%02d:%02d/n",
st.wYear ,
st.wMonth ,
st.wDay ,
st.wHour ,
st.wMinute ,
st.wSecond);

st.wYear=StrToInt(szFileTime,0,3);
st.wMonth=StrToInt(szFileTime,4,5);
st.wDayOfWeek=1; //此处的值不要更改
st.wDay=StrToInt(szFileTime,6,7);
st.wHour=StrToInt(szFileTime,8,9);
st.wMinute=StrToInt(szFileTime,10,11);
st.wSecond=StrToInt(szFileTime,12,13);
st.wMilliseconds=0; //此处的值不要更改

SystemTimeToFileTime(&st,&ft_localTime);

printf("ChangedTime:%04d-%02d-%02d %02d:%02d:%02d/n",
st.wYear ,
st.wMonth ,
st.wDay ,
st.wHour ,
st.wMinute ,
st.wSecond);

LocalFileTimeToFileTime(&ft_localTime,&lpCreationTime);
LocalFileTimeToFileTime(&ft_localTime,&lpLastAccessTime);
LocalFileTimeToFileTime(&ft_localTime,&lpLastWriteTime);

bResult=SetFileTime(hFile,NULL,NULL,&lpLastWriteTime);

if(bResult)
{
printf("File Time Set Succeed!/n");
CloseHandle(hFile);
return 0;
}

else
{
printf("File Time Set Failed!/n%d/n",GetLastError());
CloseHandle(hFile);
return 1;
}

CloseHandle(hFile);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐