【问题标题】:Inno Setup get directory size including subdirectoriesInno Setup 获取目录大小,包括子目录
【发布时间】:2017-05-30 19:43:36
【问题描述】:

我正在尝试编写一个返回目录大小的函数。我已经编写了以下代码,但它没有返回正确的大小。例如,当我在 {pf} 目录上运行它时,它返回 174 字节,这显然是错误的,因为该目录的大小是数 GB。这是我的代码:

function GetDirSize(DirName: String): Int64;
var
  FindRec: TFindRec;
begin
  if FindFirst(DirName + '\*', FindRec) then
    begin
      try
        repeat
          Result := Result + (Int64(FindRec.SizeHigh) shl 32 + FindRec.SizeLow);
        until not FindNext(FindRec);
      finally
        FindClose(FindRec);
      end;
    end
  else
    begin
      Result := -1;
    end;
end;

我怀疑FindFirst 函数不包含子目录,这就是我没有得到正确结果的原因。因此,如何返回正确的目录大小,即包括所有子目录中的所有文件,就像在 Windows 资源管理器中选择文件夹上的属性一样?我正在使用FindFirst,因为该函数需要支持超过 2GB 的目录大小。

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    FindFirst 确实包含子目录,但它不会为您提供它们的大小。

    您必须递归到子目录并逐个文件计算总大小,例如Inno Setup: copy folder, subfolders and files recursively in Code section。

    function GetDirSize(Path: String): Int64;
    var
      FindRec: TFindRec;
      FilePath: string;
      Size: Int64;
    begin
      if FindFirst(Path + '\*', FindRec) then
      begin
        Result := 0;
        try
          repeat
            if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
            begin
              FilePath := Path + '\' + FindRec.Name;
              if (FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
              begin
                Size := GetDirSize(FilePath);
              end
                else
              begin
                Size := Int64(FindRec.SizeHigh) shl 32 + FindRec.SizeLow;
              end;
              Result := Result + Size;
            end;
          until not FindNext(FindRec);
        finally
          FindClose(FindRec);
        end;
      end
        else
      begin
        Log(Format('Failed to list %s', [Path]));
        Result := -1;
      end;
    end;
    

    对于Int64,您需要Unicode version of Inno Setup,无论如何您都应该使用它。只有当您有充分的理由坚持使用 Ansi 版本时,您才可以将 Int64 替换为 Integer,但仅限于 2 GB。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 2021-10-30
      • 2017-05-13
      相关资源
      最近更新 更多