Board index » delphi » TP7: Getting # of Files and Total Bytes

TP7: Getting # of Files and Total Bytes

I was wondering how I could get the Total # of files in a Directory, and how
to get the total # of Bytes used in that Directory?  Thanks, Sam.
 

Re:TP7: Getting # of Files and Total Bytes


Quote
Sam Evans (sev...@primenet.com) wrote:
> I was wondering how I could get the Total # of files in a Directory, and
> how to get the total # of Bytes used in that Directory?  Thanks, Sam.

Somewhat simple...

Uses Dos;

Var TotSize  : LongInt;
    TotFiles : Word;

Procedure DirCount;
Var sr:SearchRec;
Begin
  FindFirst('*.*',AnyFile,Sr); TotSize:=0; TotFiles:=0;
  While DosError=0 do Begin
    if (Sr.Attr and (Directory or VolumeID))=0 then Begin
      Inc(TotFiles); Inc(TotSize,Sr.Size);
    End;
    FindNext(Sr);
  End;
End;

Begin
  DirCount;
  WriteLn(TotFiles,' file(s), ',TotSize,' byte(s)');
End.

--
Jean-Christophe Boggio                          c...@gestalt.freenix.fr
Pascal and Linux {*word*60}, Dos and Windows independant programmer.

Re:TP7: Getting # of Files and Total Bytes


Quote
Jean-Christophe Boggio (c...@gestalt.freenix.fr) wrote:

: Sam Evans (sev...@primenet.com) wrote:
: > I was wondering how I could get the Total # of files in a Directory, and
: > how to get the total # of Bytes used in that Directory?  Thanks, Sam.

: Somewhat simple...

: Uses Dos;

: Var TotSize  : LongInt;
:     TotFiles : Word;

: Procedure DirCount;
: Var sr:SearchRec;
: Begin
:   FindFirst('*.*',AnyFile,Sr); TotSize:=0; TotFiles:=0;
:   While DosError=0 do Begin
:     if (Sr.Attr and (Directory or VolumeID))=0 then Begin
:       Inc(TotFiles); Inc(TotSize,Sr.Size);
:     End;
:     FindNext(Sr);
:   End;
: End;

: Begin
:   DirCount;
:   WriteLn(TotFiles,' file(s), ',TotSize,' byte(s)');
: End.

This is a nice code, but did you take into account the size of the cluster?

Re:TP7: Getting # of Files and Total Bytes


Quote
You wrote:
> Jean-Christophe Boggio (c...@gestalt.freenix.fr) wrote:
> : Sam Evans (sev...@primenet.com) wrote:
> : > I was wondering how I could get the Total # of files in a Directory, and
> : > how to get the total # of Bytes used in that Directory?  Thanks, Sam.
> This is a nice code, but did you take into account the size of the cluster?

Not so nice, I forgot the variables initialization code :-(

I'm used to display cluster adjusted sizes in Kb because cluster sizes are
always (except maybe on some/all floppies ?) multiples of 1024. It's also
shorter and easier to read/distinguish.

Uses Dos;

Var TotSize   : LongInt;
    RealSize  : LongInt; {+++}
    ClustSize : LongInt; {+++}
    TotFiles  : Word;

Function ClusterSize(c:Char):Word;
Type DPB = Record
        Numero     : Byte;
        SousUnite  : Byte;
        OctPSect   : Word;
        Interleave : Byte;
        SectPClust : Byte;
        Reserves   : Word;
        NbreDeFats : Byte;
        RootDir    : Word;
        PremSect   : Word;
        DernClust  : Word;
        SectPFat   : Byte;
        DataSect1  : Word;
        Driver     : Pointer;
        Media      : Byte;
        Flag       : Byte;
        NextDPB    : Pointer;
     End;
Var  p:^DPB;
     b:Byte;
     r:Registers;
Begin
  b:=Byte(UpCase(c)); ClusterSize:=0;
  if b in [$41..$5A] then Begin
    b:=b-$40;
    With r do Begin
      Ah:=$32; Dl:=b; MsDos(r);
      p:=Ptr(ds,bx);
      if Al=0 then With p^ do ClusterSize:=(1 shl SectPClust)*OctPSect;
    End;
  End;
End;

Procedure DirCount;
Var sr:SearchRec;
Begin
  FindFirst('*.*',AnyFile,Sr); TotSize:=0; TotFiles:=0;
  While DosError=0 do Begin
    if (Sr.Attr and (Directory or VolumeID))=0 then Begin
      Inc(TotFiles); Inc(TotSize,Sr.Size);
      Inc(RealSize,(Sr.Size+ClustSize-1) div ClustSize); {+++}
    End;
    FindNext(Sr);
  End;
End;

Begin
  ClustSize:=ClusterSize('C');          {+++}
  TotSize:=0; RealSize:=0; TotFiles:=0; {+++}
  DirCount;
  WriteLn(TotFiles,' file(s), ',TotSize,' byte(s) (',
          RealSize div 1024,' Kb)');
End.

--
Jean-Christophe Boggio                          c...@gestalt.freenix.fr
Pascal and Linux {*word*60}, Dos and Windows independant programmer.

Re:TP7: Getting # of Files and Total Bytes


Quote
c...@gestalt.freenix.fr (Jean-Christophe Boggio) wrote:
>You wrote:
>> Jean-Christophe Boggio (c...@gestalt.freenix.fr) wrote:
>> : Sam Evans (sev...@primenet.com) wrote:
>> : > I was wondering how I could get the Total # of files in a Directory, and
>> : > how to get the total # of Bytes used in that Directory?  Thanks, Sam.
>> This is a nice code, but did you take into account the size of the cluster?
>Not so nice, I forgot the variables initialization code :-(
>I'm used to display cluster adjusted sizes in Kb because cluster sizes are
>always (except maybe on some/all floppies ?) multiples of 1024. It's also
>shorter and easier to read/distinguish.
>Uses Dos;
>Var TotSize   : LongInt;
>    RealSize  : LongInt; {+++}
>    ClustSize : LongInt; {+++}
>    TotFiles  : Word;
>Function ClusterSize(c:Char):Word;
>Type DPB = Record
>    Numero     : Byte;
>    SousUnite  : Byte;
>    OctPSect   : Word;
>    Interleave : Byte;
>    SectPClust : Byte;
>    Reserves   : Word;
>    NbreDeFats : Byte;
>    RootDir    : Word;
>    PremSect   : Word;
>    DernClust  : Word;
>    SectPFat   : Byte;
>    DataSect1  : Word;
>    Driver     : Pointer;
>    Media      : Byte;
>    Flag       : Byte;
>    NextDPB    : Pointer;
>     End;
>Var  p:^DPB;
>     b:Byte;
>     r:Registers;
>Begin
>  b:=Byte(UpCase(c)); ClusterSize:=0;
>  if b in [$41..$5A] then Begin
>    b:=b-$40;
>    With r do Begin
>      Ah:=$32; Dl:=b; MsDos(r);
>      p:=Ptr(ds,bx);
>      if Al=0 then With p^ do ClusterSize:=(1 shl SectPClust)*OctPSect;
>    End;
>  End;
>End;
>Procedure DirCount;
>Var sr:SearchRec;
>Begin
>  FindFirst('*.*',AnyFile,Sr); TotSize:=0; TotFiles:=0;
>  While DosError=0 do Begin
>    if (Sr.Attr and (Directory or VolumeID))=0 then Begin
>      Inc(TotFiles); Inc(TotSize,Sr.Size);
>      Inc(RealSize,(Sr.Size+ClustSize-1) div ClustSize); {+++}
>    End;
>    FindNext(Sr);
>  End;
>End;
>Begin
>  ClustSize:=ClusterSize('C');          {+++}
>  TotSize:=0; RealSize:=0; TotFiles:=0; {+++}
>  DirCount;
>  WriteLn(TotFiles,' file(s), ',TotSize,' byte(s) (',
>          RealSize div 1024,' Kb)');
>End.
>--
>Jean-Christophe Boggio                              c...@gestalt.freenix.fr
>Pascal and Linux {*word*60}, Dos and Windows independant programmer.

Except on 360K (and smaller) floppies where it is 512 bytes. Now that
I think about it I'm not sure what it is on 720K floppies.

Other Threads