The RenameFile function

The RenameFile function renames a file.

Parameters: DS:AX - pointer to the string with the current file name; ES:BX - pointer to the string with the new file name.

Return value: On success (CF=0) the file is renamed; on error (CF=1) AX contains the error code.

Flag state: The function affects all flags.

Program source code

An example program for the Intel 8086 assembler demonstrating the use of the RenameFile function to change the name of a file.

title Author: Evgeny Sergeevich Dovgopolov

.model small
.stack 100h

data segment public
    fOldName db "test.txt", 0
    fNewName db "main.txt", 0
    rSuccess db "The file was successfully renamed to '%s'.\0"
    rError   db "An error occurred while renaming '%s' to '%s'. Error code: %d\0"
data ends

code segment public                ; Start of the code segment
    assume  ds:data, cs:code
    include Handyfar.inc           ; Include the file with functions

    main proc                      ; Start of the main procedure
        call    SetDataSegment     ; Call the SetDataSegment procedure

        lea     bx, es:[fNewName]  ; 2nd: Load the address of the string fNewName into the es:bx register
        lea     ax, ds:[fOldName]  ; 1st: Load the address of the string fOldName into the ds:ax register
        call    RenameFile         ; Call the RenameFile procedure (ds:ax, es:bx) := AX and CF

        jnc     NoRenameFileError  ; If CF=0, jump to the label NoRenameFileError

        push    ax                 ; 4th: Save the ax register value (error code) on the stack
        lea     ax, ds:[fNewName]  ; Load the address of the variable fNewName into the ds:ax register
        push    ax                 ; 3rd: Save the ax register value on the stack
        lea     ax, ds:[fOldName]  ; Load the address of the variable fOldName into the ds:ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[rError]    ; 1st: Load the format string rError into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:rError, ...)
        add     sp, 6              ; Align the stack
        jmp     Done               ; Perform an unconditional jump to the label Done

    NoRenameFileError:
        lea     ax, ds:[fNewName]  ; Load the address of the variable fNewName into the ds:ax register
        push    ax                 ; 2nd: save the ax register value on the stack
        lea     ax, ds:[rSuccess]  ; 1st: Load the address of the format string rSuccess into the ds:ax register
        call    PrintFormatString  ; Call the PrintFormatString procedure (ax:rSuccess, fNewName)
        add     sp, 2              ; Align the stack

    Done:
        xor     al, al             ; Clear the al register
        call    ExitProcess        ; Call the ExitProcess procedure
    main endp                      ; End of the main procedure

code ends                          ; End of the code segment
end main	                       ; End of file. The main procedure is the entry point

The file was successfully renamed to 'main.txt'.

Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".