Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2005.02.13;
Скачать: [xml.tar.bz2];

Вниз

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

 
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;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.46 MB
Время: 0.041 c
3-1106045971
Mortal
2005-01-18 13:59
2005.02.13
Альтернатива DBGrid.


1-1107259415
Tornado
2005-02-01 15:03
2005.02.13
Где найти DBChart???


1-1106837943
Денис Шаров
2005-01-27 17:59
2005.02.13
Как поставить "Hook" на CDROM???


4-1103808045
Alek
2004-12-23 16:20
2005.02.13
Посылка сообщений!


3-1105534884
sergey18
2005-01-12 16:01
2005.02.13
Опять про даты в MSSQL, но другое





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский