Board index » delphi » Getting the size of a directory/folder?

Getting the size of a directory/folder?

Hey all,
I was wondering if someone could point me to the function call that will be
able to tell me how big a directory/folder is. I've searched through the
Help files and so far have come up empty. Any help would appreciated.

-Ross Elliott

 

Re:Getting the size of a directory/folder?


Hi Ross,

Try using this function. It returns a record of type TDirInfo. Declare a variable of this type and
use it like this.
  variable := DirInfo(DirName, RecurseSubDirectories);
Then query the variable identifiers:
  number of files in directory(s) := variable.files
  number of directories := variable.dirs
  total size of all files in directory(s) := variable.size

type
  TDirInfo = record
    files: Integer;
    dirs: Integer;
    size: Integer;
  end;

function DirInfo(const Dirname: TFilename; Subs: Boolean): TDirInfo;
var
  rec: TSearchRec;
  code: Integer;
begin
  with Result do begin
    files := 0;
    dirs := 0;
    size := 0;
  end;
  code := FindFirst(Dirname + '\*.*', faAnyfile and not faVolumeID, rec);
  while code = 0 do begin
    Inc(Result.size, rec.size);
    if ((rec.attr and faDirectory) <> 0) then begin
      if (rec.name[1] <> '.') then begin
        Inc(Result.dirs);
        if Subs then with DirInfo(Dirname + '\' + rec.name, True), Result do begin
          Inc(files, files);
          Inc(dirs, dirs);
          Inc(size, size);
        end;
      end;
    end else
      Inc(Result.files);
    code := FindNext(rec);
  end;
end;

Hope this is of some help

Simon Patterson

Quote
Ross Elliott <RossDElli...@yahoo.com> wrote in message news:7h28ar$257s$1@news.gate.net...
> Hey all,
> I was wondering if someone could point me to the function call that will be
> able to tell me how big a directory/folder is. I've searched through the
> Help files and so far have come up empty. Any help would appreciated.

> -Ross Elliott

Re:Getting the size of a directory/folder?


Thanks Simon,
That works great!

-Ross Elliott

Quote
Simon Patterson <si...@pogo36.freeserve.co.uk> wrote in message

news:7h2f87$50v$1@news6.svr.pol.co.uk...
Quote
> Hi Ross,

> Try using this function. It returns a record of type TDirInfo. Declare a

variable of this type and
Quote
> use it like this.
>   variable := DirInfo(DirName, RecurseSubDirectories);
> Then query the variable identifiers:
>   number of files in directory(s) := variable.files
>   number of directories := variable.dirs
>   total size of all files in directory(s) := variable.size

> type
>   TDirInfo = record
>     files: Integer;
>     dirs: Integer;
>     size: Integer;
>   end;

> function DirInfo(const Dirname: TFilename; Subs: Boolean): TDirInfo;
> var
>   rec: TSearchRec;
>   code: Integer;
> begin
>   with Result do begin
>     files := 0;
>     dirs := 0;
>     size := 0;
>   end;
>   code := FindFirst(Dirname + '\*.*', faAnyfile and not faVolumeID, rec);
>   while code = 0 do begin
>     Inc(Result.size, rec.size);
>     if ((rec.attr and faDirectory) <> 0) then begin
>       if (rec.name[1] <> '.') then begin
>         Inc(Result.dirs);
>         if Subs then with DirInfo(Dirname + '\' + rec.name, True), Result
do begin
>           Inc(files, files);
>           Inc(dirs, dirs);
>           Inc(size, size);
>         end;
>       end;
>     end else
>       Inc(Result.files);
>     code := FindNext(rec);
>   end;
> end;

> Hope this is of some help

> Simon Patterson

> Ross Elliott <RossDElli...@yahoo.com> wrote in message

news:7h28ar$257s$1@news.gate.net...

- Show quoted text -

Quote
> > Hey all,
> > I was wondering if someone could point me to the function call that will
be
> > able to tell me how big a directory/folder is. I've searched through the
> > Help files and so far have come up empty. Any help would appreciated.

> > -Ross Elliott

Other Threads