Set the time in the file attribute, including: Creation Time, access time, modification time

Source: Internet
Author: User
Tags filetime

// 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 ==================================== =========================/n ");
Printf ("/T/tget & setfiletime ver0.1 by mxlong/n ");
Printf ("/T ++ ++/n ");
Printf ("/T/tusage:/N ");
Printf ("/T/tgetfiletime: maid./N ");
Printf ("/T/texample: Maid mxlong.txt/n ");
Printf ("/T/tsetfiletime: Maid filetime./N ");
Printf ("/T/texample: gstime mxlong.txt 19820704213000/n ");
Printf ("/T/t19820704213000 = 21:30:00/n ");
Printf ("/T ++ ++/N ");
Printf ("/T/tqq: 289362563/T/tdate: 2006.7.20/N ");
Printf ("/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 (Creation Time)
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 (modification time)
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 (access time)
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; // system time
Filetime ft_localtime; // file time Temporary Variable
Filetime lpcreationtime; // Creation Time
Filetime lplastaccesstime; // access time
Filetime lplastwritetime; // modification time
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; // do not change the value here
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; // do not change the value here

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.