![]() |
| ||
|
|
Новости |
Поиск |
FAQ |
Статьи |
Книги |
Новости VCL
| | Форумы ЧАТ |
Орешник |
|
| |||
| |||
« предыдущая ветвь | форум | следующая ветвь »
Как получить размер папки? [D7, WinXP]
rolex © (2005-01-30 15:22:00)Делаю по такому коду, а размер получается неверным!
uses FileCtrl;
function DirSize(Dir: string): integer;
var
SearchRec: TSearchRec;
Separator: string;
DirBytes: integer;
begin
Result:=-1;
if Copy(Dir,Length(Dir),1)='\' then
Separator := ''
else
Separator := '\';
if FindFirst(Dir+Separator+'*.*',faAnyFile,SearchRec) = 0 then
begin
if FileExists(Dir+Separator+SearchRec.name) then
DirBytes := DirBytes + SearchRec.Size
else
if DirectoryExists(Dir+Separator+SearchRec.name) then
begin
if (SearchRec.name<>'.') and (SearchRec.name<>'..') then
DirSize(Dir+Separator+SearchRec.name);
end;
while FindNext(SearchRec) = 0 do
begin
if FileExists(Dir+Separator+SearchRec.name) then
DirBytes := DirBytes + SearchRec.Size
else
if DirectoryExists(Dir+Separator+SearchRec.name) then
begin
if (SearchRec.name<>'.') and (SearchRec.name<>'..') then
DirSize(Dir+Separator+SearchRec.name);
end;
end;
end;
FindClose(SearchRec);
Result:=DirBytes;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
DirBytes: integer;
begin
DirBytes := DirSize('c:\windows');
Form1.Label1.Caption := IntToStr(DirBytes);
end;
Fay (2005-01-30 20:20:00) [1]2 Anatoly Podgoretsky © (30.01.05 19:50) [2]
Это Вы мне? Так я знаю. GetFileSize рулит 8)
rolex (2005-01-30 20:01:00) [2]
> Anatoly Podgoretsky © (30.01.05 19:50) [2]
> У папки нет размера, это запись в файловой системе.
> Откуда такой дикий код.
Ну тогда я некорректно выразился. Но я думаю все поняли, что мне требуется.
А код я уже нашёл! Вот:
procedure GetDirSize(const aPath: string; var SizeDir: Int64);
var
SR: TSearchRec;
tPath: string;
begin
Application.ProcessMessages;
tPath := IncludeTrailingBackSlash(aPath);
if FindFirst(tPath + '*.*', faAnyFile, SR) = 0 then
begin
try
repeat
Application.ProcessMessages;
if (SR.Name = '.') or (SR.Name = '..') then
Continue;
if (SR.Attr and faDirectory) <> 0 then
begin
GetDirSize(tPath + SR.Name, SizeDir);
Continue;
end;
SizeDir := SizeDir +
(SR.FindData.nFileSizeHigh shl 32) +
SR.FindData.nFileSizeLow;
until FindNext(SR) <> 0;
finally
Application.ProcessMessages;
Sysutils.FindClose(SR);
end;
end;
end;
Anatoly Podgoretsky (2005-01-30 19:50:00) [3]У папки нет размера, это запись в файловой системе.
Откуда такой дикий код.
Fay (2005-01-30 19:32:00) [4]А так?
function GetDirSize(cDir : string) : Int64;
var
fh : THandle;
fd : _WIN32_FIND_DATAA;
sz : LARGE_INTEGER;
fn : string;
begin
Result := 0;
fh := FindFirstFile(PChar(cDir + '\*'), fd);
if fh <> INVALID_HANDLE_VALUE then
try
repeat
if (fd.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0 then
begin
fn := Trim(fd.cFileName);
if (fn <> '.') and (fn <> '..') then
Inc(Result, GetDirSize(cDir + '\' + fn));
end
else
begin
sz.HighPart := fd.nFileSizeHigh;
sz.LowPart := fd.nFileSizeLow;
Inc(Result, sz.QuadPart);
end;
until not FindNextFile(fh, fd);
finally
Windows.FindClose(fh);
end;
end;