Obtaining the size of a file on disk

Basic and Machine Language

Moderator: Moderators

Post Reply
KingTrode
Vic 20 Hobbyist
Posts: 133
Joined: Tue Apr 13, 2010 2:32 am

Obtaining the size of a file on disk

Post by KingTrode »

I'm attempting to obtain the size of a disk file in bytes before it is processed.

Unfortunately my knowledge of Commodore DOS is still quite poor. :roll:

Apart from opening a file and reading it byte by byte and keeping a counter until EOF is reached, is there a quicker way to get the exact size in bytes?


Thanks


Richard
User avatar
Mike
Herr VC
Posts: 4841
Joined: Wed Dec 01, 2004 1:57 pm
Location: Munich, Germany
Occupation: electrical engineer

Re: Obtaining the size of a file on disk

Post by Mike »

There exists a faster method, but that critically depends on knowledge built into the program about the directory, and sector-link structure, specific to 1541/1571/1581, etc.

You would:

- extract the start track/sector of the file from the directory entry,
- load the sector from disc,
- add 254 to file length if a chains another sector,
- otherwise add the bytes indicated if it is the last sector.

This still requires the drive to fetch all sector data from disc, however the transfers from drive to computer are minimized.

I'll take a look into a CBM DOS whether the knowledge about the dir structure could be amended - i.e. if one can read the S/T data of the first block from the drive after a regular file open.
User avatar
Schlowski
NoMess!
Posts: 892
Joined: Tue Jun 08, 2004 12:20 pm

Re: Obtaining the size of a file on disk

Post by Schlowski »

- otherwise add the bytes indicated if it is the last sector.
Add the bytes -2 as the sector data here points to the last byte used in this sector, this includes the first two link bytes - that confused me when I made my first attempts to read files from disk.

Other than that this is exactly the procedure I follow in BasEdit's ShowDirectory routine for diskimages :-)
Of course on the PC this is lightning fast as opposed to read a lot of sectors on the real hardware...
Post Reply