Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2005.02.13;
Скачать: CL | DM;

Вниз

Как получить размер папки?   Найти похожие ветки 

 
rolex   (2005-01-30 15:22) [0]

Делаю по такому коду, а размер получается неверным!

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 19:32) [1]

А так?

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;


 
Anatoly Podgoretsky ©   (2005-01-30 19:50) [2]

У папки нет размера, это запись в файловой системе.
Откуда такой дикий код.


 
rolex   (2005-01-30 20:01) [3]


> 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;


 
Fay ©   (2005-01-30 20:20) [4]

2 Anatoly Podgoretsky ©   (30.01.05 19:50) [2]
Это Вы мне? Так я знаю. GetFileSize рулит 8)



Страницы: 1 вся ветка

Текущий архив: 2005.02.13;
Скачать: CL | DM;

Наверх




Память: 0.48 MB
Время: 0.044 c
8-1098891463
Colonel
2004-10-27 19:37
2005.02.13
Как програмно увеличить/уменьшить звук ?


1-1106817512
Dysan
2005-01-27 12:18
2005.02.13
thread и тормоза программы


6-1101480735
Muhan
2004-11-26 17:52
2005.02.13
Как выудить данные с интернет страницы с помощью ссылки в Делфи


3-1105790252
moor38
2005-01-15 14:57
2005.02.13
Excel в paradox?!?!?!


14-1106665010
kirasukii
2005-01-25 17:56
2005.02.13
PR-технологии и demo-кратия. Дилемма.