如何通过"修改日期"确定Windows目录中的哪个文件夹是最新的?
喜欢这个,但对于文件夹而不是文件.
我需要创建一个类似的函数 GetLastModifiedFolderName('D:\LogFolder\'):string;
在下面的一些建议(来自MBo)然后阅读一些findfirst引用.我修改了已回答的链接变得像这样:
function TForm1.GetLastModifiedFolderName(AFolder: String): string;
var
sr: TSearchRec;
aTime: Integer;
begin
Result := '';
aTime := 0;
if FindFirst(IncludeTrailingPathDelimiter(AFolder)+'*',faDirectory, sr) = 0 then
begin
// directory found
repeat
if (sr.Attr and faDirectory)=faDirectory then
begin
// directory only
if (sr.Name <> '.') and (sr.name<>'..') then
begin
// exclude '.' and '..' directory
if sr.Time > aTime then
begin
aTime := sr.Time;
Result := sr.Name;
end;
end;
end;
until FindNext(sr) <> 0;
FindClose(sr);
end else
begin
// not found
Result:='-1';
end;
end;
Run Code Online (Sandbox Code Playgroud)
您可以使用链接答案稍加修改的功能.因为您只需要文件夹,只需检查文件系统对象(由FindXX函数找到)是否为目录:
if (sr.Attr and faDirectory) = faDirectory ...
Run Code Online (Sandbox Code Playgroud)
PS请注意,新的Delphi版本包含System.IOUtils单元,其中包含各种有用的方法.