INT 21h AH=41h removes a file from its directory. The name is ASCIIZ and may include a drive and a path. It must name one file: this function takes no wildcards, so to delete *.TMP a program finds each match with INT 21h AH=4Eh / 4Fh and deletes it by name.
| AX | Meaning |
|---|---|
| 02h | file not found |
| 03h | path not found: a directory in the name does not exist |
| 05h | access denied: the file is read-only, the name is a directory (those go with INT 21h AH=3Ah) or a device |
MOV AH, 3Ch ; DOS: create TEST.TXTXOR CX, CX ; CX = 0: normal attributesMOV DX, OFFSET FNAMEINT 21hCALL CHECK ; stop if CF = 1MOV BX, AX ; BX = the handleMOV AH, 3Eh ; close it firstINT 21hMOV DX, OFFSET M1MOV AH, 09h ; DOS: print the string at DS:DXINT 21hMOV AH, 41h ; DOS: delete TEST.TXTMOV DX, OFFSET FNAMEINT 21hCALL REPORTMOV DX, OFFSET M2MOV AH, 09h ; DOS: print the string at DS:DXINT 21hMOV AH, 41h ; the same name againMOV DX, OFFSET FNAMEINT 21hMOV DX, OFFSET GONE1 ; CF = 1: nothing by that nameJC SHOW1MOV DX, OFFSET ODD1SHOW1:MOV AH, 09hINT 21hFNAME DB "TEST.TXT", 0M1 DB "first delete: $"M2 DB "second delete: $"GONE1 DB "CF = 1, there is nothing left to delete", 13, 10, "$"ODD1 DB "CF = 0?", 13, 10, "$"CRLF DB 13, 10, "$"CHECK PROC NEAR ; CF = 1: print the error code in AL and exitJNC CHECK1PUSH AXMOV DX, OFFSET FAILMSGMOV AH, 09hINT 21hPOP AXCALL PHEXBMOV AX, 4C01h ; exit with code 1INT 21hCHECK1:RETCHECK ENDPFAILMSG DB "failed: error $"REPORT PROC NEAR ; CF = 0: "ok"; CF = 1: "error" and ALJC REPORT1MOV DX, OFFSET OKMSGMOV AH, 09hINT 21hRETREPORT1:PUSH AXMOV DX, OFFSET ERRMSGMOV AH, 09hINT 21hPOP AXCALL PHEXB ; the error codeMOV DX, OFFSET CRLFMOV AH, 09hINT 21hRETREPORT ENDPOKMSG DB "ok", 13, 10, "$"ERRMSG DB "error $"PHEXB PROC NEAR ; print AL as two hex digitsPUSH CXPUSH DXMOV DH, AL ; keep the byteMOV CL, 4SHR AL, CL ; the high digit firstMOV CX, 2PHEXB1:AND AL, 0FhADD AL, "0"CMP AL, "9"JBE PHEXB2ADD AL, 7 ; 10..15 -> "A".."F"PHEXB2:MOV DL, ALMOV AH, 02h ; DOS: print DLINT 21hMOV AL, DH ; then the low digitLOOP PHEXB1POP DXPOP CXRETPHEXB ENDP
On MS-DOS the second delete returns AX = 02h, file not found. DOSBox-X, on a host folder, can leave an earlier access-denied code in AX instead, which is why the example tests only CF.
What happens on a FAT disk: DOS marks the directory entry free by overwriting the first character of the name with E5h, and frees the file's clusters in the FAT. The data stays on the disk until something reuses those clusters, which is how UNDELETE can bring a file back if you are quick.
Pitfalls
Close a file before deleting it. Without SHARE, MS-DOS deletes an open file without complaint, and later writes through the stale handle land in clusters that now belong to nobody, or to another file. DOSBox-X, on a host folder, closes the program's handles to the file and then deletes it. A read-only file must have its attribute cleared with INT 21h AH=43h first.