确定文件夹中哪个文件是最新的“修改日期”? [英] Determining which file in a folder is the latest by "modified date"?
问题描述
我需要扫描特定文件夹中的最新文件(基本上检查修改日期以查看最新的日期),但请记住,文件具有随机名称。这是到目前为止我得到的:
I need to scan a specific folder for the latest file (basically check the modified date to see which is the newest), But keep in mind that the files have random names. Here's what I got so far:
procedure TForm1.Button1Click(Sender: TObject);
begin
ftp.Host := 'domain';
ftp.Username := 'username';
ftp.password := 'password';
ftp.Connect;
ftp.Put('random-filename.ext'); //This is where it should grab only the latest file
ftp.Quit;
ftp.Disconnect;
end;
这可能吗?
谢谢!
推荐答案
假设OP想要扫描特定的本地文件夹并找到最新的修改文件,这是一个非常简单的功能只是这样:
Assuming that OP wants to scan specific local folder and find the most recent modified file, here's a very simple function to do just that:
function GetLastModifiedFileName(AFolder: String; APattern: String = '*.*'): String;
var
sr: TSearchRec;
aTime: Integer;
begin
Result := '';
aTime := 0;
if FindFirst(IncludeTrailingPathDelimiter(AFolder) + APattern, faAnyFile, sr) = 0 then
begin
repeat
if sr.Time > aTime then
begin
aTime := sr.Time;
Result := sr.Name;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
AFolder
应该是绝对路径还是相对路径对于要扫描的文件夹, APattern
是可选的,并且应包含一个标准的DOS模式,该模式指定应检查的文件。如果没有为第二个参数指定任何内容,则假定为*。*(所有文件)。结果将是具有最近修改日期的文件名。
AFolder
should be an absolute or relative path to a folder you want to scan, APattern
is optional and should contain a standard DOS pattern that specifies which files should be checked. If nothing is specified for 2nd parameter, *.* (all files) is assumed. Result will be the file name that has the most recent modified date.
这篇关于确定文件夹中哪个文件是最新的“修改日期”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!