现在的位置: 首页 > 综合 > 正文

文件时间设置与读取的代码

2013年09月18日 ⁄ 综合 ⁄ 共 4713字 ⁄ 字号 评论关闭
//文件:Get&SetFileTime.cpp
//版本:0.1
//时间:2006.7.20
//描述:可以设置文件属性里的时间,包括:创建时间、访问时间、修改时间
//环境:Win2000+sp4+VC++6.0

#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(strlen(argv[2])!=14)
   {
       printf("Parameter '%s' Error!Time string length is 14./n",argv[2]);
   }
       
else
   {
       if(argc==2)_GetFileTime(argv[1]);
 else
       {
           if(argc==3)_SetFileTime(argv[1],argv[2]);
       }
   }

   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");
   exit(0);
}

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,&lpCreationTime,&lpLastAccessTime,&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;
}

抱歉!评论已关闭.