急,怎么获得一个文件夹中最新创建的那个文件用Delphi实现,望各位指点,最好能提供其代码或者范例!
解决方案 »
- advstringgrid1的MergeCells功能
- 数据库连接字符串含变量问题
- 用Delphi,能取得浏览器级别(就是关掉浏览器,Cookie就没有了)的cookie吗?
- 很烦啊,想找人说个话,坐了一天,血都不流动了
- 谁有delphi6的安装序列号
- delphi编程的问题,很着急,请帮帮我
- ADO问题——:”初始化提供者时发生错误,测试连接失败。无法启动应用程序。工作组信息文件丢失,或是已被其他用户以独占方式打开。”
- 模拟键盘按键的问题!
- 100分求一个好的控件!关于对于javascript的解释的控件,不要微软的ActiveX组件ScriptControl!!!!
- 求助:delphi爱好者看一下,看看怎么解决最好!!拜托!拜托!
- [免费软件]局域网监控及文件同步工具
- IDA逆向Delphi程序时很多的库函数认不出来,请问要怎么弄?
function CovFileDate(Fd:_FileTime):TDateTime;
{ 转换文件的时间格式 }
var
Tct:_SystemTime;
Temp:_FileTime;
begin
FileTimeToLocalFileTime(Fd,Temp);
FileTimeToSystemTime(Temp,Tct);
CovFileDate:=SystemTimeToDateTime(Tct);
end;procedure TForm1.Button2Click(Sender: TObject);
const
Model='yyyy/mm/dd'; { 设定时间格式 }
var
Tp:TSearchRec; { 申明Tp为一个查找记录 }
T1,T2,T3:string;
begin
FindFirst('D:\SQL10.TXT',faAnyFile,Tp); { 查找目标文件 }
T1:=FormatDateTime(Model,CovFileDate(Tp.FindData.ftCreationTime));
{ 返回文件的创建时间 }
T2:=FormatDateTime(Model,CovFileDate(Tp.FindData.ftLastWriteTime));
{ 返回文件的修改时间 }
T3:=FormatDateTime(Model,Now);
{ 返回文件的当前访问时间 }
FindClose(Tp);
end;然后再查找日期最靠前的那个就行
procedure TForm1.getfiletime(filepathandname: string;var cT, at, wt: TFileTime);
Var
Q : HFile;
begin
Q := 0;
Try
Q := FileOpen(FilepathandName, fmOpenRead);
if Q <= 0 then
ShowMessage('Open File Error!!!')
Else
GetFileTime(Q,PFileTime(@cT),PFileTime(@at),PFileTime(@wt));
finally
FileClose(Q);
end;
end;
BOOL GetFileTime(
HANDLE hFile, // handle to the file
LPFILETIME lpCreationTime, // address of creation time
LPFILETIME lpLastAccessTime, // address of last access time
LPFILETIME lpLastWriteTime // address of last write time
);
function FindNewByCreationTime(path:string):string;
var
sr:TSearchRec;
lpSystemTime:SystemTime;
localFT:FileTime;
dt, LatestTime:TDateTime;
filename:string;
begin
if FindFirst(path+'\*.*',faAnyFile,sr)=0 then//API函数FindFirst用于查找文件
begin
repeat
if (sr.Name<>'.') and (sr.Name<>'..') then
begin
if (sr.attr and faDirectory)<>0 then//是目录,就递归
FindNewByCreationTime(path+'\'+sr.Name)
else begin
FileTimeToLocalFileTime(sr.FindData.ftCreationTime,localFT);
FileTimeToSystemTime(LocalFT,lpSystemTime);
dt := SystemTimeToDateTime(lpSystemTime);
if (dt > LatestTime) then
begin
filename := path+'\'+sr.Name;
LatestTime:= dt;
end;
end;
end;
until FindNext(sr)<>0;
SysUtils.FindClose(sr);
end;
end;
GetFileTime(Q,PFileTime(@cT),PFileTime(@at),PFileTime(@wt));
ct保存的是创建时间
at是最后打开时间
wt是最后写入时间
var
FFileName: String;function TMainForm.GetDirectoryName(Dir: String): String;
begin
if Dir[Length(Dir)]<> '\' then
Result := Dir+'\'
else
Result := Dir;
end;procedure TMainForm.FindFiles(APath: String);
var
FSearchRec,
DSearchRec: TSearchRec;
FindResult: integer; function IsDirNotation(ADirName: String): Boolean;
begin
Result := (ADirName = '.') or (ADirName = '..');
end;begin
APath := GetDirectoryName(APath);
FindResult := FindFirst(APath+FFileName,faAnyFile+faHidden+
faSysFile+faReadOnly,FSearchRec);
try
while FindResult = 0 do
begin
memo1.Items.Add(LowerCase(APath+FSearchRec.Name));//保存你找到的文件
...//在这里获取每个文件的创建时间 或在外部根据 memo1中文件的进行处理
FindResult := FindNext(FSearchRec);
end;
FindResult := FindFirst(APath+'*.*', faDirectory, DSearchRec);
while FindResult = 0 do
begin
if ((DSearchRec.Attr and faDirectory) = faDirectory) and not
IsDirNotation(DSearchRec.Name) then
FindFiles(APath+DSearchRec.Name);
FindResult := FindNext(DSearchRec);
end;
finally
FindClose(FSearchRec);
end;
end;procedure TMainForm.btnSearchForFilesClick(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
try
lbFiles.Items.Clear;
FFileName := edtFileMask.Text;
FindFiles(dolDirectories.Directory);
finally
Screen.Cursor := crDefault;
end;
end;procedure TMainForm.dcbDrivesChange(Sender: TObject);
begin
dolDirectories.Drive := dcbDrives.Drive;
end;end.