設定檔案屬性裡的時間,包括:建立時間、訪問時間、修改時間

來源:互聯網
上載者:User

// 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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.