• delphi获取文件的创建/修改时间、按时间删除指定文件下的文件


    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,DateUtils;
    ...
    //获取文件的创建时间
    function 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;
     
    //获取文件的修改时间
    function 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;
     
    调用:
    edit1.text:=datetimetostr( GetFileEditTime('E:\test\D12000398C1.csv'));
     
    获取文件夹下的所有文件名称、按文件的修改时间删除指定的文件
    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);
        //这样filelist中就保存了你输入的文件夹下的所有文件,我下面显示在一个memo组件中了
        for i:=0 to filelist.count-1 do
        begin
          if hoursBetween(GetFileEditTime('E:\test\'+filelist[i]),Now)>48  then
             begin
                try
                deletefile('E:\test\'+filelist[i]);
                except
                end;
             end;
           //memo1.lines.text:=memo1.lines.text+filelist[i]+#13;
        end;
        filelist.free;
    end;
  • 相关阅读:
    CentOS查看系统信息和资源使用已经升级系统的命令
    192M内存的VPS,安装Centos 6 minimal x86,无法安装node.js
    Linux限制资源使用的方法
    多域名绑定同一IP地址,Node.js来实现
    iOS 百度地图大头针使用
    iOS 从app跳转到Safari、从app打开电话呼叫
    设置cell背景色半透明
    AsyncSocket 使用
    iOS 监听键盘变化
    iOS 7 标签栏控制器进行模态视图跳转后变成透明
  • 原文地址:https://www.cnblogs.com/jijm123/p/15587613.html
Copyright © 2020-2023  润新知