通过“修改日期”确定目录中的哪个文件夹是最新的?

时间:2014-03-14 03:50:17

标签: delphi

如何通过“修改日期”确定Windows目录中的哪个文件夹是最新的? Like this one,但文件夹不是文件。

我需要创建一个像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;

1 个答案:

答案 0 :(得分:5)

您可以使用链接答案稍加修改的功能。因为您只需要文件夹,只需检查文件系统对象(由FindXX函数找到)是否为目录:

if (sr.Attr and faDirectory) = faDirectory ...

P.S。请注意,新的Delphi版本包含System.IOUtils单元,其中包含各种有用的方法。

相关问题