RenameFileEx function
The RenameFileEx function renames a file. Pointers to the old and new names are passed through the stack. The function automatically aligns the stack to 4 bytes.
Parameters (stack): 1. Pointer to the current name. 2. Pointer to the new 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
Intel 8086 assembly program example demonstrating the use of the RenameFileEx function to rename a file. The names are passed through the stack.
title .286C .model small .stack 100h data segment public fOldName db "test.txt", 0 fNewName db "main.txt", 0 rSuccess db "The file was renamed successfully to '%s'.\0" rError db "An error occurred while renaming '%s' to '%s'. Error code: %d\0" data ends code segment public assume ds:data, cs:code include Handyfar.inc ; Include the file with functions include Handymac.inc ; Include the file with macros main proc call SetDataSegment ; Call the SetDataSegment procedure pushs es:[fNewName] ; 2nd: Push the address of variable fNewName onto the stack pushs ds:[fOldName] ; 1st: Push the address of variable fOldName onto the stack call RenameFileEx ; Call procedure RenameFileEx(fOldName, fNewName) := AX and CF jnc @NoRenameFileError ; If CF=0, jump to label @NoRenameFileError push ax ; 4th: Push ax (error code) onto the stack pushs ds:[fNewName] ; 3rd: Push the address of variable fNewName onto the stack pushs ds:[fOldName] ; 2nd: Push the address of variable fOldName onto the stack leaax ds:[rError] ; 1st: Load the address of format string rError into register ds:ax call PrintFormatString ; Call procedure PrintFormatString(ax:rError, ...) add sp, 6 ; Align the stack jmp @Done ; Perform an unconditional jump to label @Done @NoRenameFileError: pushs ds:[fNewName] ; 2nd: Push the address of variable fNewName onto the stack leaax ds:[rSuccess] ; 1st: Load the address of format string rSuccess into register ds:ax call PrintFormatString ; Call procedure PrintFormatString(ax:rSuccess, fNewName) add sp, 2 ; Align the stack @Done: push 0 ; Push the byte return code onto the top of the stack call ExitProcessEx ; Call the ExitProcessEx(0) procedure main endp code ends end main
The file was renamed successfully to 'main.txt'.
Example taken from the textbook "HANDYFAR - MASM (Evgeny Sergeevich Dovgopolov)".