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;
原文地址:https://www.cnblogs.com/zhangzhifeng/p/9073600.html