Subject: Size of a file 80. ***** Q: How do I find out the size of any kind of a file? A1: Well, to begin with the FileSize keyword and an example code are given in the manual (and help function of later TP versions) so those, as usual, are the first places to look at. But the example solution can be somewhat improved, and there is also an alternative solution. The FSIZEFN should never be applied on an open file. function FSIZEFN (filename : string) : longint; var fle : file of byte; { declare as a file of byte } fmSave : byte; begin fmSave := FileMode; { save the current filemode } FileMode := 0; { to handle also read-only files } assign (fle, filename); {$I-} reset (fle); {$I+} { to do your own error detection } if IOResult <> 0 then begin fsizefn := -1; FileMode := fmSave; exit; end; fsizefn := FileSize(fle); close (fle); FileMode := fmSave; { restore the original filemode } end; (* fsizefn *) A2: The second, general alternative is uses Dos; function FSIZE2FN (FileName : string) : longint; var FileInfo : SearchRec; { SearchRec is declared in the Dos unit } begin fsize2fn := -1; { return -1 if anything goes wrong } FindFirst (filename, AnyFile, FileInfo); if DosError <> 0 then exit; if (FileInfo.Attr and VolumeId = 0) and (FileInfo.Attr and Directory = 0) then fsize2fn := FileInfo.Size; end; (* fsize2fn *) A3: The third alternative is due to a Usenet posting by Wayne Hoxsie (hoxsiew@crl.com). This alternative is an instructive example of using file handles. uses dos; var f : file; {} function filelength (var f : file) : longint; var handle : ^word; regs : registers; begin handle := @f; fillchar (regs, SizeOf(regs), 0); { just in case } regs.ax := $4202; regs.bx := handle^; regs.cx := 0; regs.dx := 0; msdos(regs); filelength := (longint(regs.dx) SHL 16)+regs.ax; end; {} begin assign(f,paramstr(1)); filemode := 0; { read-only files too } reset(f); writeln(filelength(f)); close(f); end. --------------------------------------------------------------------