Uses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls,
dateutils;...
//Get File creation timefunction Getfilecreationtime (const filename:string): Tdatetime;var filetime:tfiletime; Localfiletime:tfiletime; Hfile:thandle; Systemtime:tsystemtime;begin Result: = 0; Filetime.dwlowdatetime: = 0; Filetime.dwhighdatetime: = 0; hfile: = FileOpen (FileName, Fmsharedenynone); Try if hfile <> 0 THEN BEGIN Windows.getfiletime (hfile, @FileTime, nil, nil); FileTimeToLocalFileTime (FileTime, localfiletime); FileTime: = Localfiletime; End Finally FileClose (hfile); End If FileTimeToSystemTime (FileTime, SystemTime) then Result: = Systemtimetodatetime (SystemTime); end;
//Get File modification timefunction Getfileedittime (const filename:string): Tdatetime;var filetime:tfiletime; Localfiletime:tfiletime; Hfile:thandle; Systemtime:tsystemtime;begin Result: = 0; Filetime.dwlowdatetime: = 0; Filetime.dwhighdatetime: = 0; hfile: = FileOpen (FileName, Fmsharedenynone); Try if hfile <> 0 THEN BEGIN Windows.getfiletime (hfile, nil, nil, @FileTime); FileTimeToLocalFileTime (FileTime, localfiletime); FileTime: = Localfiletime; End Finally FileClose (hfile); End If FileTimeToSystemTime (FileTime, SystemTime) then Result: = Systemtimetodatetime (SystemTime); end;
Call:Edit1.text:=datetimetostr (Getfileedittime (' E:\test\D12000398C1.csv '));
Get all the file names under the folder, delete the specified files by the modified time of the files Procedure Tform1.button2click (sender:tobject);var r:TsearchRec; filelist:Tstringlist; i:integer;begin filelist:=tstringlist.create; if FindFirst (' e:\test\ ' + ' *.csv ' , faanyfile,r) =0 then Filelist.add (r.name); while FindNext (R) =0 do Filelist.add (r.name); FindClose (R); //So all the files in the folder you entered are saved in filelist, I am shown below in a memo component for i:=0 to Filelist.count-1 do begin If Hoursbetween (Getfileedittime (' E:\test\ ' +filelist[i]), Now) >48 then begin try &NB Sp DeleteFile (' E:\test\ ' +filelist[i]); except &NB Sp end; end; //memo1.lines.text:= memo1.lines.text+filelist[i]+ #13; end; FileliSt.free;end;
Delphi Get file creation/modification time, delete files under specified file by time