2017-01-16 11 views
1

디렉토리의 크기를 반환하는 함수를 작성하려고합니다. 다음 코드를 작성했지만 올바른 크기를 반환하지 않습니다. 예를 들어, {pf} 디렉토리에서 실행하면 174 바이트가 반환됩니다.이 디렉토리는 크기가 여러 기가 바이트이므로 분명히 잘못되었습니다. 여기 내가 가지고있는 코드 :Inno 설치 하위 디렉토리를 포함한 디렉토리 크기를 얻습니다.

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 탐색기에서 폴더의 속성을 선택하는 것과 동일합니까? 기능이 2GB 이상의 디렉토리 크기를 지원해야하므로 FindFirst을 사용하고 있습니다.

답변

1

FindFirst에는 하위 디렉토리가 포함되어 있지만 크기는 알 수 없습니다.

예를 들어 Inno Setup: copy folder, subfolders and files recursively in Code section과 같이 하위 디렉토리를 반복적으로보고 파일별로 총 크기를 계산해야합니다. Int64를 들어


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; 

, 당신은 어떤 경우에 사용되어야 하는지를, Unicode version of Inno Setup해야합니다. Ansi 버전을 사용하는 것이 좋은 이유가있는 경우에만 Int64Integer으로 바꿀 수 있지만 2GB로 제한됩니다.

+0

버전 2.2이고 Int64는 인식 할 수 없습니다. 어떤 대안을 사용할 수 있습니까? – Brian

+0

@Brian 버전 2.2 무엇? –

+0

2.2 inno setup studio – Brian