Time when files are obtained and modified in Delphi

Source: Internet
Author: User
Tags filetime
First of all, I found this article in a post when searching for relevant information on csdn, so I cannot find the source. I hope the original author will not blame it...

This document describes how to use system functions and Windows API functions in Delphi to obtain and modify the time information of a file.

If you are familiar with Windows 95/98, you will often use the right-click method to view the properties of the selected file. The file creation time, modification time, and access time are listed in the properties menu. These information is often very useful. Their settings are generally automatically completed by the operating system (that is, by DOS/windows, etc.) and won't be easily modified by users.

Here, I will introduce how to obtain and modify the file time in Delphi. Delphi provides a complete interface for calling Windows API functions to facilitate advanced windows programming. The findfirst function in Delphi can be used to obtain the attribute record of a file. The finddata field in the record records detailed file time information. Unfortunately, the time information in finddata cannot be obtained directly. Therefore, I have compiled a conversion function to convert the time format of the file. The specific implementation method is provided below for your reference only:
Function covfiledate (FD: _ filetime): tdatetime;
{Time format of the converted file}
VaR
TCT: _ systemtime;
Temp: _ filetime;
Begin
Filetimetolocalfiletime (FD, temp );
Filetimetosystemtime (temp, TCT );
Covfiledate: = systemtimetodatetime (TCT );
End;
With the above function support, we can get the time information of a file. The following is a simple example:
Procdeure getfiletime (const TF: string );
{Get file time. TF indicates the path and name of the target file}
Const
Model = 'yyyy/MM/DD, HH: mm: ss'; {set the time format}
VaR
TP: tsearchrec; {declare TP as a search record}
T1, T2, T3: string;
Begin
Findfirst (TF, faanyfile, TP); {find target file} T1: = formatdatetime (model,
Covfiledate (Tp. finddata. ftcreationtime )));
{Returned file creation time}
T2: = formatdatetime (model,
Covfiledate (Tp. finddata. ftlastwritetime )));
{Response file modification time}
T3: = formatdatetime (model, now ));
{Current access time of the returned file}
Findclose (TP );
End;
The time for setting files is more complex. Here we will introduce how to use the datatimepicker component in Delphi to assist in this complex operation. The following example uses four datatimepicker components to set the File Creation Time and modification time. Note: The file access time is replaced by the modification time. In the following example, add four datatimepicker components to your form. Kind in the first and third datatimepicker components is set to dtkdate, and kind in the second and fourth datatimepicker components is set to dtktime.
Procedure setfiledatetime (const TF: string );
{Set the file time. TF indicates the path and name of the target file}
VaR
Dt1, dt2: integer;
FS: tfilestream;
FCT, Flt: tfiletime;
Begin
Dt1: = datetimetofiledate (
Trunc (form1.datetimepicker1. Date) + frac (form1.datetimepicker2. Time ));
Dt2: = datetimetofiledate (
Trunc (form1.datetimepicker3. Date) + frac (form1.datetimepicker4. Time ));
{Convert user input information in datatimepicker}
Try
FS: = tfilestream. Create (TF, fmopenreadwrite );
Try
If dosdatetimetofiletime (longrec (dt1). Hi, longrec (dt1). Lo, FCT) and
Localfiletimetofiletime (FCT, FCT) and
Dosdatetimetofiletime (longrec (dt2). Hi, longrec (dt2). Lo, Flt) and
Localfiletimetofiletime (FLT, Flt)
Then setfiletime (FS. handle,
@ FCT, @ FLT, @ Flt );
{Set file time attribute}
Finally
FS. Free;
End;
Except
Messagedlg ('date modification operation failed! ',
Mterror, [mbok], 0 );
{Failure due to reasons such as the target file being used}
End;
End;
The preceding section briefly describes how to modify the file time attribute. Note: The modification time range of the file starts from January 1, September 19, 1792 AD, and the maximum modification time can reach January 1, 2999 AD or higher. In addition, do not use this technology to corrupt others' documents or other improper channels.

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.