Delphi - 从指定路径中获取最后创建的文件夹名称

3

有没有一个函数可以从给定路径中获取最后创建的文件夹?我想要查看最后创建的文件夹,以便检查我的相机今天是否拍摄了照片。另一种方法是获取系统日期,然后开始搜索包含当前日期的文件夹。但是,如果相机日期不正确,则此方法将无法正常工作!谢谢。还有其他想法吗?

例如:

if lastcreatedfolder(dir_path):='05012016' then 
showmessage('TODAY A FOLDER WAS CREATED') 
else 
showmessage('NO FOLDER WAS CREATED TODAY!');

1
没有这样的函数。您需要枚举目录并根据您确定的任何标准找到最新的目录。 - David Heffernan
谢谢。那我想我必须枚举所有文件夹,获取它们的创建日期,排序日期并获取最后一个日期。 - user2858981
4
不需要排序,只需枚举文件夹并记录遇到的最新文件夹。排序的时间复杂度为O(n log n),查找最大值的时间复杂度为O(n)。 - David Heffernan
3个回答

5

Delphi 2010也有IOUtils.pas单元。

使用该单元,可以通过以下方式找到最后创建的文件夹:

uses
  IOUtils, Types, DateUtils;

function FindLastCreatedDirectory(const APath: string): string;
var
  LastCreateTime : TDateTime;
  PathsInQuestion: TStringDynArray;
  n : Integer;
begin
  LastCreateTime := MinDateTime;
  Result := '';

  PathsInQuestion := TDirectory.GetDirectories(APath);
  for n := Low(PathsInQuestion) to High(PathsInQuestion) do
  begin
    if CompareDateTime(TDirectory.GetCreationTime(PathsInQuestion[n]), LastCreateTime) = GreaterThanValue then
    begin
      LastCreateTime := TDirectory.GetCreationTime(PathsInQuestion[n]);
      Result := PathsInQuestion[n];
    end;
  end;
end;

谢谢您发布这段代码。然而,存在一个问题,结果不准确。例如,我有两个文件夹:2015-12-28创建于2015年12月29日18:22:00(根据Windows7资源管理器),以及2015-12-29创建于2015年12月29日18:21:54。您的函数显示最后创建的项目是2015-12-29。 - user2858981
@user2858981 是的,你说得对。我用CompareDateTime函数稍微修改了一下,所以它能够正常工作——希望在你那边也是这样。 - Christian Holm Jørgensen
谢谢,现在它运行正常! 它还有一个优点,可以列出给定路径的子文件夹。 - user2858981

2
在给定路径中,可以使用System.SysUtils.FindFirst函数找到最后创建的目录。
可以使用函数的var F参数检查TSearchRec记录的TimeStamp字段,以评估文件系统元素的时间戳。请注意保留HTML标签。
uses
  System.SysUtils,
  Winapi.Windows;

function getLastCreatedDirectory(const APath: string): string;
var
  res: TSearchRec;
  lastCreatedFileTime: TFileTime;
begin
  Result := '';
  FillChar(lastCreatedFileTime, SizeOf(TFileTime), 0);

  if FindFirst(APath, faDirectory, res) = 0 then begin
    try
      repeat
        if (res.Attr and faDirectory) = 0 then
          Continue;

        if (res.Name = '.') or (res.Name = '..') then
          Continue;

        {if res.TimeStamp > lastCreatedTime then begin
          lastCreatedTime := res.TimeStamp;
          Result := ExtractFilePath(APath) + res.Name;
        end;}

        if CompareFileTime(res.FindData.ftCreationTime, lastCreatedFileTime) = 1 then begin
          lastCreatedFileTime := res.FindData.ftCreationTime;
          Result := ExtractFilePath(APath) + res.Name;
        end;

      until FindNext(res) <> 0;
    finally
      System.SysUtils.FindClose(res);
    end;
  end;
end;

begin
  WriteLn(getLastCreatedDirectory('C:\Program Files (x86)\*'));
  ReadLn;
end.

编辑2
由于res.TimeStamp似乎提供了最后修改日期,而TSearchRec.Time字段已被弃用,因此可以通过评估TSearchRec记录的res.FindData.ftCreationTime字段来获取文件夹的创建时间。


在delphi2010中,我必须将“res.Timestamp”更改为“res.time”才能编译。此外,如果我创建一个文件夹并在其中放置一个文件,它将显示此文件夹。如果我删除最后创建的文件夹,则此函数将不会显示剩余文件夹中最后创建的文件夹。 - user2858981
嗯...在我的XE7 sysutils单元中,res.time已经被弃用。我将使用GetFileAttributesEx函数来编辑答案,这可能更适合您的需求。 - fantaghirocco
为了强制仅列出目录,我添加了// Only show directories if (res.attr and faDirectory) = faDirectory then begin"。谢谢fantaghirocco。这是一个很好的解决方案! - user2858981
糟糕,我错过了文档中说“Attr参数指定特殊文件,除了所有正常文件外还要包括在内”的部分。根据您的评论进行了编辑,谢谢。 - fantaghirocco

-2

如果文件夹是由您自己创建的,即使名称是随机字符,也可以将该文件夹名称保存在数组中。但是,如果文件夹是由程序或其他方式创建的,则可以进入该文件夹。

cd "path"
    exp: cd /home

使用“bellow”命令按创建日期对文件和文件夹进行排序,

因此,如果有许多文件,请使用此命令:

    ls -lt | less

我认为'head'命令对你也会有用


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接