Board index » delphi » Getting a files size (in bytes)?

Getting a files size (in bytes)?

Can anyone give me a strategy on how to get the number of bytes in a file
(any file that the user picks on their HD) from within Delphi?

Thanks
Mike

 

Re:Getting a files size (in bytes)?


Quote
M wrote in message <6a7erv$...@ns2.spectra.net>...
>Can anyone give me a strategy on how to get the number of bytes in a file
>(any file that the user picks on their HD) from within Delphi?

Mike:

Here's one way:

  FUNCTION GetFileSize(CONST FileName:  STRING):  LongInt;
    VAR
      FileModeSave:  BYTE;
      RawFile     :  FILE OF Byte;
  BEGIN
    TRY
      FileModeSave := System.FileMode;
      System.FileMode := fmOpenRead + fmShareDenyNone; {file access, read
only}

      AssignFile (RawFile, FileName);
      Reset (RawFile);
      RESULT := FileSize(RawFile);
      CloseFile (RawFile);

      System.FileMode := FileModeSave
    EXCEPT
      ShowMessage ('Fatal Error:  Cannot open ' + FileName + ' (binary)');
      Halt
    END
  END {GetFileSize};

Another common way to get this information is to look in the TSeachRec
record returned by FindFirst or FindNext.

efg
_________________________________________________

Earl F. Glynn          EarlGl...@WorldNet.att.net
MedTech Research Corporation
Lenexa, KS  66219  USA

Re:Getting a files size (in bytes)?


M schrieb in Nachricht <6a7erv$...@ns2.spectra.net>...

Quote
>Can anyone give me a strategy on how to get the number of bytes in a
file
>(any file that the user picks on their HD) from within Delphi?

>Thanks
>Mike

Ther are some complicated ways, but I would use FileSize in unit
SysUtils (or in System?)

Re:Getting a files size (in bytes)?


M heeft geschreven in bericht <6a7erv$...@ns2.spectra.net>...
|Can anyone give me a strategy on how to get the number of bytes in a
file
|(any file that the user picks on their HD) from within Delphi?
|
try:

function GetFileSize(AFileName: string) : integer; [ longint if D1
!! ]
var
 f : TFilestream;
begin
  try
   f := TFileStream.Create(AFileName, fmOPENREAD);
   Result := f.Size;
   f.free;
  except
   Result := -1; // oops...
  end;
end;

Other Threads