how to get the folder size using ant script

If you need help on how to use the program
Post Reply
jete2011
Posts: 1
Joined: 2013-07-13 12:03:54

how to get the folder size using ant script

Post by jete2011 »

i copied a folder from src to destination.but i need to verify whether the size
of the folder from both source and destination are equal
soulsnake
Posts: 756
Joined: 2011-03-14 15:42:20
Location: France

Post by soulsnake »

Hi jete2011,

There is no native fonction to do this but you can use the function GetFolderSize below to do what you need.
Just copy it in your script as it is done in example below.

Code: Select all

program ShowFolderSize;
var
  size: Double;

  function GetFolderSize(path: string): double;
  var
    s: string;
    lines: TStringList;
    values: TStringListEx;
    i: integer;
  begin
    result := 0;
    path := IncludeTrailingPathDelimiter(path);
    lines := TStringList.Create;
    values := TStringListEx.Create;
    values.Delimiter := #9;
    if DirectoryExists(path) then
    begin
      // List current folder
      s := ListDirectory(path, '*');
      // Extract lines
      lines.Text := s;
      // For all lines
      for i := 0 to lines.Count-1 do
      begin
        // Extract values of line (name, size, datetime, "D" if directory)
        values.DelimitedCSVText := lines.GetString(i);
        while values.Count < 4 do
          values.Add('');
        if (values.GetString(0) <> '') and
           (values.GetString(0) <> '.') and
           (values.GetString(0) <> '..') then
        begin
          // If it is a folder, we compute the size of this folder recursively
          if values.GetString(3) = 'D' then
            result := result + GetFolderSize(path + values.GetString(0))
          // Else we add the filesize to result
          else
          begin
            // We use function GetFileSize to get good filesize because the size returned
            // by function ListDirectory is only good for small files < 2 147 483 648 bytes
            if FileExists(path + values.GetString(0)) then
              result := result + GetFileSize(path + values.GetString(0))
            else
              result := result + StrToFloat(values.GetString(1));
          end;
        end;
      end;
    end;
    lines.Free;
    values.Free;
  end;
begin
  // Example
  size := GetFolderSize('C:\Movies\');
  ShowMessage(FloatToStr(size) + ' bytes');
end.
Soulsnake.
Post Reply