Inno Setup 计数目录中具有给定文件扩展名的文件

Inno Setup Count files in a directory with given file extension

我想在 Inno Setup 中计算目录中具有给定文件扩展名的文件数。我写了下面的代码。

此致,

function AmountOfFilesInDir(const DirName, Extension: String): Integer;
var
  FilesFound: Integer;
  ShouldBeCountedUppercase, ShouldBeCountedLowercase: Boolean;
  FindRec: TFindRec;
begin
  FilesFound := 0;
  if FindFirst(DirName, FindRec) then begin
    try
      repeat
        if (StringChangeEx(FindRec.Name, Lowercase(Extension), Lowercase(Extension), True) = 0) then
          ShouldBeCountedLowercase := False
        else
          ShouldBeCountedLowercase := True;
        if (StringChangeEx(FindRec.Name, Uppercase(Extension), Uppercase(Extension), True) = 0) then
          ShouldBeCountedUppercase := False
        else
          ShouldBeCountedUppercase := True;
        if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY = 0)
           and ((ShouldBeCountedUppercase = True) or (ShouldBeCountedLowercase = True)) then begin
          FilesFound := FilesFound + 1;
        end;
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
  Result := FilesFound;
end;

用法示例可以是:

Log(IntToStr(AmountOfFilesInDir(ExpandConstant('{sys}\*'), '.exe')));

我想减少此函数代码中的行数,使其看起来更专业,因为它看起来有点长。我需要知道如何在不使此功能失败的情况下执行此操作。

提前致谢。

FindFirst function 可以 select 自己基于通配符(扩展名)的文件:

function AmountOfFiles(PathWithMask: string): Integer;
var
  FindRec: TFindRec;
begin
  Result := 0;
  if FindFirst(PathWithMask, FindRec) then
  begin
    try
      repeat
        Inc(Result);
      until not FindNext(FindRec);
    finally
      FindClose(FindRec);
    end;
  end;
end;

像这样使用它:

Log(IntToStr(AmountOfFiles(ExpandConstant('{sys}\*.exe'))));