80: How do I find out the size of any kind of a file?
A: 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 *)
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 *)