Inno Setup 获取包括子目录在内的目录大小
Inno Setup get directory size including subdirectories
我正在尝试编写一个 return 目录大小的函数。我已经编写了以下代码,但它 return 的大小不正确。例如,当我 运行 它位于 {pf}
目录时,它 return 有 174 个字节,这显然是错误的,因为这个目录的大小是几千兆字节。这是我的代码:
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
函数不包含子目录,这就是我没有得到正确结果的原因。因此,我怎样才能 return 一个目录的正确大小,即包括所有子目录中的所有文件,就像在 Windows 资源管理器中选择文件夹上的属性一样?我正在使用 FindFirst
,因为函数需要支持超过 2GB 的目录大小。
FindFirst
确实包含子目录,但不会为您提供它们的大小。
您必须递归到子目录并逐个文件计算总大小,类似于 。
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
,您需要 ,无论如何您都应该使用。仅当您有充分理由坚持使用 Ansi 版本时,才可以将 Int64
替换为 Integer
,但限制为 2 GB。
我正在尝试编写一个 return 目录大小的函数。我已经编写了以下代码,但它 return 的大小不正确。例如,当我 运行 它位于 {pf}
目录时,它 return 有 174 个字节,这显然是错误的,因为这个目录的大小是几千兆字节。这是我的代码:
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
函数不包含子目录,这就是我没有得到正确结果的原因。因此,我怎样才能 return 一个目录的正确大小,即包括所有子目录中的所有文件,就像在 Windows 资源管理器中选择文件夹上的属性一样?我正在使用 FindFirst
,因为函数需要支持超过 2GB 的目录大小。
FindFirst
确实包含子目录,但不会为您提供它们的大小。
您必须递归到子目录并逐个文件计算总大小,类似于
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
,您需要 Int64
替换为 Integer
,但限制为 2 GB。