Board index » delphi » Need Help Using File Attributes

Need Help Using File Attributes

I have torn apart the documentation and cannot find a clear example of
how to get the size in bytes of various files in a directory.
The closest I've come is the TSearchRec, but I can't seem to find any
examples or information on how to actually use it.

Could someone give me some hints, or a small snippet of code?

Thank you in advance
--
Joe ;-)

*************************
If you call a dog's tail a leg  
- How many legs does it have?
Hint: Answer is *not* 42.

 

Re:Need Help Using File Attributes


Quote
Joe Greenwald wrote:

> I have torn apart the documentation and cannot find a clear example of
> how to get the size in bytes of various files in a directory.
> The closest I've come is the TSearchRec, but I can't seem to find any
> examples or information on how to actually use it.

> Could someone give me some hints, or a small snippet of code?

> Thank you in advance
> --
> Joe ;-)

> *************************
> If you call a dog's tail a leg
> - How many legs does it have?
> Hint: Answer is *not* 42.

Off the top of my head -

var
        srchRec : TSearchRec;
        iErrCode : Integer;
begin
        iErrCode := FindFirst('C:\*.*',faAnyFile,srchRec);
        try

                while iErrCode = 0 do
                begin
                        DoSomeStuff(srchRec.Size);
                        iErrCode := FindNext(srchRec);
                end;

                if not (iErrCode = -18) then
                        raise Exception.Create('Error reading directory');
        finally
                FindClose(srchRec);
        end;

end;

As to the dog question, I am a sucker - is it not NumberOfCurrentLegs + 1?

Cya

--
Mark.

Re:Need Help Using File Attributes


In article <3228BDFB.6...@earthlink.com>,
   Joe Greenwald <jgreenw...@earthlink.com> wrote:
]-I have torn apart the documentation and cannot find a clear example of
]-how to get the size in bytes of various files in a directory.
]-The closest I've come is the TSearchRec, but I can't seem to find any
]-examples or information on how to actually use it.
]-
]-Could someone give me some hints, or a small snippet of code?

there are several ways you might do it...

  o  open the file as an untyped file, then use the
     FileSize function

          assign(f, filename);
          reset(f, 1);
          SizeOfFile := FileSize(f);
          close(f);

  o  create a TFileStream object...

          fs := TFileStream.Create(filename);
          SizeOfFile := fs.size;
          fs.Free;

  o  use the FindFirst function; one of the fields in the
     TSearchRec variable passed to FindFirst will, on return,
     contain the file size

          if (FindFirst(filename, faAnyFile, MySearchRec) = 0) then
            SizeOfFile := MySearchRec.size

Mark Vaughan

]-
]-Thank you in advance

Re:Need Help Using File Attributes


In article 6...@earthlink.com, Joe Greenwald <jgreenw...@earthlink.com> writes:

Quote
>I have torn apart the documentation and cannot find a clear example of
>how to get the size in bytes of various files in a directory.
>The closest I've come is the TSearchRec, but I can't seem to find any
>examples or information on how to actually use it.

>Could someone give me some hints, or a small snippet of code?

>Thank you in advance

>Joe ;-)

Look up FindFirst, FindNext, FindClose to SearchRec stuff. Also FileSize.

HTH

Ray Cramer
R...@Pol.ac.uk

Re:Need Help Using File Attributes


Please visit my Home Page at
    http://www.inet.co.th/{*word*104}mall/newlook/
looking for Delphi Page then
 d2dirdir.zip (14,103 bytes as 24 Jun 96)
     FOR: Delphi 2.0 only.
     USAGE: Directory with total file size.
     DEMO: Recursion
     USES: TTreeView, TListView and TImageList.
it may be help you
  Regards
   Boonlert
---------------

Quote
Joe Greenwald wrote:

> I have torn apart the documentation and cannot find a clear example of
> how to get the size in bytes of various files in a directory.
> The closest I've come is the TSearchRec, but I can't seem to find any
> examples or information on how to actually use it.

> Could someone give me some hints, or a small snippet of code?

> Thank you in advance
> --
> Joe ;-)

] *************************
] If you call a dog's tail a leg
] - How many legs does it have?
] Hint: Answer is *not* 42.

Other Threads