Author: Jonas Bilinkevicius
How to get the date a file was created
Answer:
Solve 1:
1 uses
2 Windows, Systutils;
3
4 function GetFileCreateDate(TheFile: string): TDateTime;
5 var
6 SearchRec: TSearchRec;
7 DT: TFileTime;
8 ST: TSystemTime;
9 begin
10 Result := 0;
11 try
12 if (FindFirst(TheFile, faAnyFile, SearchRec) = 0) then
13 begin
14 FileTimeToLocalFileTime(SearchRec.FindData.ftCreationTime, DT);
15 FileTimeToSystemTime(DT, ST);
16 Result := EncodeDate(st.wYear, st.wMonth, st.wDay) +
17 EncodeTime(st.wHour, st.wMinute, st.wSecond, 0);
18 end;
19 finally
20 FindClose(SearchRec);
21 end;
22 end;
Solve 2:
23
24 {This function returns the file creation timestamp of the specified file.
25 Uses-clause order dependency: if "Windows" is used, it must come before "SysUtils".}
26
27 function GetFileCreationTimestamp(const FileName: string): TDateTime;
28 var
29 SearchRec: TSearchRec;
30 begin
31 if (FindFirst(Filename, faAnyfile, SearchRec) = 0) then
32 begin
33 FindClose(SearchRec);
34 if ((SearchRec.FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0) then
35 Result := UTCFileTimeToLocalTimestamp(SearchRec.FindData.ftCreationTime)
36 else
37 raise Exception.Create('GetFileCreationTimestamp: File is a directory.');
38 end
39 else
40 raise Exception.Create('GetFileCreationTimestamp: File does not exist.');
41 end;
42
43 {This function converts a TFileTime in UTC to a TDateTime for the local time zone.}
44
45 function UTCFileTimeToLocalTimestamp(const UTCFileTime: TFileTime): TDateTime;
46 var
47 LocalFileTime: TFileTime;
48 FATDateTime: LongInt;
49 begin
50 if (FileTimeToLocalFileTime(UTCFileTime, LocalFileTime)) then
51 if (FileTimeToDosDateTime(LocalFileTime, LongRec(FATDateTime).Hi,
52 LongRec(FATDateTime).Lo)) then
53 Result := FileDateToDateTime(FATDateTime)
54 else
55 raise
56 Exception.Create('UTCFileTimeToLocalTimestamp: Timestamp conversion error.')
57 else
58 raise Exception.Create('UTCFileTimeToLocalTimestamp: Timestamp conversion
59 error.'
60 end;
Solve 3:
61
62 function FileTimeToLocalDateTime(filetime: TFileTime): TDatetime;
63 var
64 LocalFileTime: TFileTime;
65 dostime: Longint;
66 begin
67 FileTimeToLocalFileTime(filetime, LocalFileTime);
68 if FileTimeToDosDateTime(LocalFileTime, LongRec(dostime).Hi, LongRec(dostime).Lo)
69 then
70 Result := FiledateToDatetime(dostime)
71 else
72 Result := 0.0;
73 end;
74
75 procedure GetFileTimes(filename: string; var creationtime, lastaccesstime,
76 lastwritetime: TDateTime);
77 var
78 srec: TSearchRec;
79 begin
80 if FindFirst(filename, faAnyfile, Srec) = 0 then
81 try
82 with SRec.FindData do
83 begin
84 creationtime := FileTimeToLocalDateTime(ftCreationTime);
85 lastaccesstime := FileTimeToLocalDateTime(ftLastAccessTime);
86 lastwritetime := FileTimeToLocalDateTime(ftLastWriteTime);
87 end;
88 finally
89 FindClose(SRec);
90 end
91 else
92 raise Exception.Create('File %s not found!', [filename]);
93 end;
You get from a TDatetime to string via Format, FormatDatetime, DateTimeToStr etc..
|