Calcule o espaço ocupado por arquivos a partir de uma pasta. Esta função recursiva faz o trabalho.
Ela varre todos os arquivos dentro de um diretório e sub-diretorios e acumula o tamanho de cada um deles, retornando no final o tamanho total acumulado.
Código fonte:
function CalculaTamanhoDir(Path : String) : Integer;
var
SR : TSearchRec;
rc : Integer;
Tamanho : Integer;
begin
Tamanho := 0;
rc := FindFirst(Path+'\*.*', faDirectory, SR);
if rc = 0 then
begin
while rc = 0 do
begin
if (SR.Name = '.') or (SR.Name = '..') then
else
if (SR.Attr and faDirectory) <> 0 then
begin
Tamanho := Tamanho + CalculaTamanhoDir(Path+SR.Name);
end
else
begin
Tamanho := Tamanho + SR.Size;
end;
rc := FindNext(SR);
end;
FindClose(SR);
end;
result := Tamanho;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TamanhoDir : Integer;
begin
TamanhoDir := CalculaTamanhoDir(ExtractFilePath(Application.ExeName));
ShowMessage(Format('%d', [TamanhoDir]));
end;