Delphi Get file creation/modification time, delete file under specified file by time

Source: Internet
Author: User
Tags filetime

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

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.