在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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;
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论