(Gast)
n/a Beiträge
|
Re: Does It Fit? Ein nützliches Brenntool
10. Aug 2007, 12:51
Ich habe eine DIRSIZE-Funktion gefunden, die identische Werte wie der Explorer anzeigt:
Delphi-Quellcode:
...
var TotalSize: Int64; //global
...
function AddThouSeps (const S: string): string;
var
LS, L2, I : Integer;
Temp : string;
begin
result := S ;
LS := Length (S);
if LS <= 3 then exit ;
L2 := (LS - 1) div 3;
Temp := '';
for I := 1 to L2 do
Temp := ThousandSeparator + Copy (S, LS - 3 * I + 1, 3) + Temp;
Result := Copy (S, 1, (LS - 1) mod 3 + 1) + Temp;
end;
function DirSize(Path: string; ScanLabel, SizeLabel: TLabel): Int64;
{ func to return the total number of bytes found in a directory. }
{ You can pass 2 TLabels for a progress while scanning:
ScanLabel: will display the current path being scanned
SizeLabel: will display the total size so far counted.
If you don't want to use either Labels, pass nil to both. You
can still use returned value to read the total size. Example:
(For Delphi 4/5)
TotalSize := 0;
Label1.Caption := IntToStr(DirSize('C:\Windows', nil, nil)) + ' bytes';
(For Delphi 3)
TotalSize := 0;
Label1.Caption := FloatToStr(DirSize('C:\Windows', nil, nil)) + ' bytes';
(It is a little faster this way.)
Note: you MUST initialize the global TotalSize variable to 0 before
using this function. }
var
Res: Integer;
SR: TSearchRec;
begin
Result := TotalSize;
if Copy(Path, Length(Path), 1) <> '\' then
Path := Path + '\';
if not DirectoryExists(Path) then
begin
MessageDlg('Directory does not exist: ' + Path, mtError, [mbOK], 0);
Exit;
end;
if ScanLabel <> nil then
begin
ScanLabel.Caption := 'Scanning ' + Path;
ScanLabel.Update;
end;
Res := FindFirst(Path + '*.*', faAnyFile, SR);
try
while Res = 0 do
begin
if (SR.Name [1] <> '.') and (SR.Name [1] <> '..') then
begin
if ((SR.Attr and faDirectory) <> 0) then
DirSize(Path + SR.Name + '\', ScanLabel, SizeLabel)
else
TotalSize := TotalSize + SR.Size;
end;
Res := FindNext(SR);
if SizeLabel <> nil then
begin
{$IFDEF Delphi3Below}
SizeLabel.Caption := 'Total size: ' + FloatToStr(TotalSize) + ' bytes';
{$ELSE}
SizeLabel.Caption := 'Total size: ' + IntToStr(TotalSize) + ' bytes';
{$ENDIF}
SizeLabel.Update;
end;
end;
finally
FindClose(SR);
end;
Result := TotalSize;
end;
//Aufruf - Beispiel:
//TotalSize:=0; Label1.Caption := FloatToStr(DirSize('C:\Windows', nil, nil)) + ' Bytes';
//oder formatiert:
//Label1.Caption := AddThouSeps(FloatToStr(DirSize('C:\Windows', scanlabel1, sizelabel1))) + ' Bytes';
|
|
Zitat
|