File size

How would I go about to determine the size of a file?


  • : How would I go about to determine the size of a file?

    At his moment I have 2 methods in my mind.

    method 1) Open the file, move to the end of it by using AX=4202h INT 21h and the file size will be at DX:AX if no error occured.

    INT 21h, function 42 (move file pointer of an open file)

    AH = 42h

    AL = 0 (move from beggining of file) or 1 (move from currect possition of file) or 2 (move from the end of file)

    BX = File handle

    CX:DX = number of bytes to move.


    ; open the file

    MOV AX,3D02h

    MOV DX,OFFSET FileName

    INT 21h

    ; Save handle in BX


    ;Move at the end of the file

    MOV AX,4202h

    XOR CX,CX ;

    XOR DX,DX ;Zero bytes from the end of file

    INT 21h

    ; now CX:DX contanis file length.

    ; close file

    MOV AH,3Eh

    INT 21h

    FileName: db 'file1.tmp',0

    Method 2) By using find first function and then getting the filesize from the DTA.

    the DTA is located at CS:0080h, but if we want to change it's location ,we can do it by using function 1Ah of INT 21h.

    FUNCTION 1Ah (Set DTA) of INT 21h

    AH = 1Ah

    DS:DX = New location of DTA (Disk Transfer Area)

    FUNCTION 4Eh (Find first matching file) of INT 21h

    AH = 4Eh

    CX = Attributes OF file to search (I thing this matters only if you are searching for volume label, so in any other situatoin use 3Fh for CX)

    DS:DX = pointer to the filename or file mask.

    and now the DTA format

    offset 15h attrubute of file (byte)

    16h File time (word)

    18h file date (word)

    1Ah file size (dword)

    1Eh name of file found


    ; in this example we will take the default DTA adress, although it's a good idea to change the DTA adress in general

    MOV AH,4Eh

    MOV CX,03Fh

    MOV DX,OFFSET FileMask

    INT 21h

    MOV DX,WORD PTR [80h + 1Ah] ; Remember 80h is the default DTA adress

    MOV CX,WORD PTR [80h + 1Ah + 2]

    ;now CX:DX contains the file size

    Hope I helped



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!