Time to get and modify files in Delphi
This article introduces the method of using system functions and Windows API function calls to obtain and modify file time information in Delphi.
Friends who are familiar with Windows 95/98 will often use the right-click method to view the attribute information of the selected file. The file creation time, modification time, and access time are listed in the properties menu. This information is often very useful, and their settings are usually automatically completed by the operating system (that is, Dos/Windows, etc.) and will not be easily modified by users.
Here, I will introduce to you how to obtain and modify file time in Delphi. Delphi provides a complete call interface for Windows API functions, which can facilitate advanced Windows programming. Using the FindFirst function in Delphi, you can get a file attribute record, and the FindData field in this record records detailed file time information. Unfortunately, the time information in FindData cannot be obtained directly. Therefore, someone (Editor's note: I'm sorry I don't know the name of this person) wrote a conversion function to complete the conversion of file time format. The specific implementation method is given below for reference only:
function CovFileDate(Fd:_FileTime):TDateTime;
{Convert file time format}
var
Tct:_SystemTime;
Temp:_FileTime;
Begin
FileTimeToLocalFileTime(Fd,Temp);
FileTimeToSystemTime(Temp,Tct);
CovFileDate:=SystemTimeToDateTime(Tct);
end;
With the above function support, we can obtain the time information of a file. Here is a simple example:
PRocdeure GetFileTime(const Tf:string);
{get file time, Tf represents the target file path and name}
const
Model=yyyy/mm/dd,hh:mm:ss; { Set time format}
var
Tp:TSearchRec; { declare Tp as a search record}
T1, T2, T3:string;
Begin
FindFirst(Tf,faAnyFile,Tp); { Find the target file} T1:=FormatDateTime(Model,
CovFileDate(Tp.FindData.ftCreationTime)));
{ Return the file creation time}
T2:=FormatDateTime(Model,
CovFileDate(Tp.FindData.ftLastWriteTime)));
{ Return the file modification time}
T3:=FormatDateTime(Model,Now));
{ Return the current access time of the file}
FindClose(Tp);
end;
The time to set a file is more complicated. Here we introduce the use of the DataTimePicker component in Delphi to assist in completing this complex operation. The following example uses four DataTimePicker components to complete the setting of file creation time and modification time. Note: The access time of the file is replaced by the modification time. When using the example below, add four DataTimePicker components to your Form. Where Kind in the first and third DataTimePicker components are set to dtkDate, and Kind in the second and fourth DataTimePicker components are set to dtkTime.
procedure SetFileDateTime(const Tf:string);
{ Set file time, Tf represents the target file path and name}
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 the information entered by the user 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);
{ Failed because the target file is being used, etc.}
end;
end;
The above briefly introduces the method of modifying file time attributes. Please note: the range of modifying file time starts on September 19, 1792, and the upper limit can reach 2999 or higher. Also, please do not use this technology for unjust ways such as destroying other people's documents.