MoveFileEx

Namespace: Win32Header: Winbase.h

Moves an existing file or directory, including its children, with options to set the move operation’s behavior.

Syntax

BOOL MoveFileExW(
    LPCWSTR lpExistingFileName,
    LPCWSTR lpNewFileName,
    DWORD   dwFlags
);

Parameters

ParameterDescription
lpExistingFileName Pointer to a null‑terminated string that specifies the name of an existing file or directory.
lpNewFileName Pointer to a null‑terminated string that specifies the new name of the file or directory. If this parameter is NULL, the function registers the file for deletion.
dwFlags Specifies how the move is to be performed. This can be a combination of the following values:
  • MOVEFILE_REPLACE_EXISTING – Replace the existing file.
  • MOVEFILE_COPY_ALLOWED – Allow the move to be performed as a copy if a rename is not possible.
  • MOVEFILE_DELAY_UNTIL_REBOOT – Register the move for execution during the next system reboot.
  • MOVEFILE_WRITE_THROUGH – Write through any intermediate cache and go directly to disk.

Return Value

Returns nonzero if the function succeeds; otherwise, 0. Use GetLastError to obtain extended error information.

Remarks

Examples

#include <windows.h>
#include <stdio.h>

int wmain(void) {
    LPCWSTR src = L"C:\\Temp\\old.txt";
    LPCWSTR dst = L"C:\\Temp\\new.txt";

    if (!MoveFileExW(src, dst, MOVEFILE_REPLACE_EXISTING)) {
        wprintf(L"Move failed. Error: %lu\\n", GetLastError());
    } else {
        wprintf(L"File moved successfully.\\n");
    }
    return 0;
}

See Also