[net.micro.cpm] File sizes from Turbo

Douglas Good <CMP.DOUG@UTEXAS-20.ARPA> (01/19/85)

Does anyone know an easy way to get file sizes in Kilobytes using TurboPascal?
If it would help I have a Kaypro computer.

		--Doug Good
-------

ihom@Nosc.ARPA (01/21/85)

 
>  Does anyone know an easy way to get file sizes in Kilobytes using
>  TurboPascal?  If it would help I have a Kaypro computer.
 
------------
In CP/M-80, the Turbo Pascal "filesize(filvar)" function will return the
number of records in the named file.  Each record consumes 128 bytes.  So,
the Kilobyte size is calculated by:  RECS x 128.  When the file is saved
in CP/M, file space is allocated in increments of 2K bytes.  The following
example will calculate the K's.
 
program find_k;
type
   filename = string[12];
var
   fname : filename;
   kfile : file;
   remain : boolean;
   i,recs,k,bytes : integer;
begin
   write('Enter the filename: ');
   readln(fname);
   for i:=1 to length(fname) do
      fname[i]:=upcase(fname[i]);
   assign(kfile,fname);
   reset(kfile);
   recs:=filesize(kfile);
   close(kfile);
   bytes:=recs*128;          { how many 128 byte records }
   k:=bytes div 1024;          { get the nearest k. oKay? }
   remain:=(bytes mod 1024)<>0;          { do I need another k? }
   if remain then k:=k+1;
   if (k=0) or odd(k) then k:=k+1;     { add another k for CP/M filesaves }
   writeln('That file is ',k,'K long')
end.


--Irwin Hom          {ihnp4, sdcsvax!bang}!crash!ihom
			   bang!crash!ihom@nosc
				bang!crash!ihom@ucsd

apratt@iuvax.UUCP (01/24/85)

The submitted algorithm has some misleading information and at least one flaw
outright: Files CAN be empty, taking up zero K on the disk. The misleading part
is that not ALL CP/M systems allocate 2k at a time; mine (Osborne DD) allocates
only 1K.  This is nice since you waste at most 1023 bytes (as opposed to 2047
in the worst case of 2K clusters), but it is NOT nice in that reading a file
>16K causes a seek back to the directory to open another extent, whereas this
would only happen every 32K with 2K allocations. 
----
						-- Allan Pratt
					...ihnp4!inuxc!iuvax!apratt