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

时间:2016-08-26 06:02:46

标签: inno-setup pascalscript

我想在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')));

我喜欢减少此功能代码中的线条,使其看起来更专业,因为它看起来有点长。我需要知道如何在不失败此功能的情况下完成。

提前致谢。

1 个答案:

答案 0 :(得分:1)

FindFirst function可以自行选择基于通配符(扩展名)的文件:

map: {
  'shared':    'apps/shared'
},
packages: {
  'shared':    {main: 'shared', defaultExtension: 'js'}
}

使用它像:

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;