MoveFileEx
Namespace: Win32 Header: 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
| Parameter | Description |
|---|---|
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:
|
Return Value
Returns nonzero if the function succeeds; otherwise, 0. Use GetLastError to obtain extended error information.
Remarks
- The function can move files across volumes if
MOVEFILE_COPY_ALLOWEDis specified. - When
lpNewFileNameisNULLandMOVEFILE_DELAY_UNTIL_REBOOTis set, the file is marked for deletion on the next reboot. - To rename a directory, both paths must end with a backslash (
\).
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;
}