Moves an existing file or directory, including its children, with various move options.
The MoveFileWithProgress function is equivalent
to the MoveFileEx function, except that
MoveFileWithProgress allows you to provide a
callback function that receives progress notifications.
To perform this operation as a transacted operation, use the
MoveFileTransacted function.
Syntax
BOOL WINAPI MoveFileEx(
__in LPCTSTR lpExistingFileName,
__in_opt LPCTSTR lpNewFileName,
__in DWORD dwFlags
);
Parameters
- lpExistingFileName [in]
-
The current name of the file or directory on the local computer.
If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT, the
file cannot exist on a remote share, because delayed operations are performed before the network is
available.
In the ANSI version of this function, the name is limited to MAX_PATH characters.
To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend
"\\?\" to the path. For more information, see
Naming a File
Windows 2000: If you prepend the file name with "\\?\", you cannot also specify the
MOVEFILE_DELAY_UNTIL_REBOOT flag for dwFlags.
- lpNewFileName [in, optional]
-
The new name of the file or directory on the local computer.
When moving a file, the destination can be on a different file system or volume. If the destination is on
another drive, you must set the MOVEFILE_COPY_ALLOWED flag in
dwFlags.
When moving a directory, the destination must be on the same drive.
If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and
lpNewFileName is NULL,
MoveFileEx registers the
lpExistingFileName file to be deleted when the system restarts. If
lpExistingFileName refers to a directory, the system removes the directory at restart
only if the directory is empty.
- dwFlags [in]
-
This parameter can be one or more of the following values.
| Value | Meaning |
|
MOVEFILE_COPY_ALLOWED 2 0x2
| If the file is to be moved to a different volume, the function simulates the move by using the
CopyFile and
DeleteFile functions.
This value cannot be used with MOVEFILE_DELAY_UNTIL_REBOOT.
|
|
MOVEFILE_CREATE_HARDLINK 16 0x10
| Reserved for future use.
|
|
MOVEFILE_DELAY_UNTIL_REBOOT 4 0x4
| The system does not move the file until the operating system is restarted. The system moves the file
immediately after AUTOCHK is executed, but before creating any paging files. Consequently, this parameter
enables the function to delete paging files from previous startups.
This value can be used only if the process is in the context of a user who belongs to the administrators
group or the LocalSystem account.
This value cannot be used with MOVEFILE_COPY_ALLOWED.
Windows 2000: If you specify the MOVEFILE_DELAY_UNTIL_REBOOT flag for
dwFlags, you cannot also prepend the file name that is specified by
lpExistingFileName with "\\?".
|
|
MOVEFILE_FAIL_IF_NOT_TRACKABLE 32 0x20
| The function fails if the source file is a link source, but the file cannot be tracked after the move.
This situation can occur if the destination is a volume formatted with the FAT file system.
|
|
MOVEFILE_REPLACE_EXISTING 1 0x1
| If a file named lpNewFileName exists, the function replaces its contents with
the contents of the lpExistingFileName file, provided that security requirements regarding access
control lists (ACLs) are met. For more information, see
the Remarks section of this topic.
This value cannot be used if lpNewFileName or
lpExistingFileName names a directory.
|
|
MOVEFILE_WRITE_THROUGH 8 0x8
| The function does not return until the file is actually moved on the disk.
Setting this value guarantees that a move performed as a copy and delete operation is flushed to disk
before the function returns. The flush occurs at the end of the copy operation.
This value has no effect if MOVEFILE_DELAY_UNTIL_REBOOT is set.
|
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0). To get extended error information, call
GetLastError.
Remarks
If the dwFlags parameter specifies
MOVEFILE_DELAY_UNTIL_REBOOT,
MoveFileEx fails if it cannot access the registry. The
function stores the locations of the files to be renamed at restart in the following registry value:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\
Session Manager\PendingFileRenameOperations
This registry value is of type REG_MULTI_SZ. Each rename operation stores one of
the following NULL-terminated strings, depending on whether the rename is a delete or not:
-
szDstFile\0\0
-
szSrcFile\0szDstFile\0
The string szDstFile\0\0 indicates that the file
szDstFile is to be deleted on reboot. The string szSrcFile\0szDstFile\0 indicates that
szSrcFile is to be renamed szDstFile on reboot.
Note Although \0\0 is technically not allowed in a REG_MULTI_SZ node, it can because
the file is considered to be renamed to a null name.
The system uses these registry entries to complete the operations at restart in the same order that they were
issued. For example, the following code fragment creates registry entries that delete
szDstFile and rename szSrcFile to be
szDstFile at restart:
MoveFileEx(szDstFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
MoveFileEx(szSrcFile, szDstFile, MOVEFILE_DELAY_UNTIL_REBOOT);
Because the actual move and deletion operations specified with the
MOVEFILE_DELAY_UNTIL_REBOOT flag take place after the calling application has ceased
running, the return value cannot reflect success or failure in moving or deleting the file. Rather, it reflects
success or failure in placing the appropriate entries into the registry.
The system deletes a directory that is tagged for deletion with the
MOVEFILE_DELAY_UNTIL_REBOOT flag only if it is empty. To ensure deletion of directories,
move or delete all files from the directory before attempting to delete it. Files may be in the directory at boot
time, but they must be deleted or moved before the system can delete the directory.
The move and deletion operations are carried out at boot time in the same order that they are specified in the
calling application. To delete a directory that has files in it at boot time, first delete the files.
If a file is moved across volumes, MoveFileEx does not
move the security descriptor with the file. The file is assigned the default security descriptor in the
destination directory.
The MoveFileEx function coordinates its operation with
the link tracking service,
so link sources can be tracked as they are moved.
To delete or rename a file, you must have either delete permission on the file or delete child permission in
the parent directory. If you set up a directory with all access except delete and delete child and the ACLs of new files are inherited, then you should be able to create a file without being able to
delete it. However, you can then create a file, and get all the access you request on the handle that is returned
to you at the time that you create the file. If you request delete permission at the time you create the file, you
can delete or rename the file with that handle but not with any other handle. For more information, see
File Security and Access Rights.
Examples
For an example, see
Creating and Using a Temporary File.
Requirements
| Minimum supported client | Windows 2000 Professional |
| Minimum supported server | Windows 2000 Server |
| Header | WinBase.h (include Windows.h) |
| Library | Kernel32.lib |
| DLL | Kernel32.dll |
| Unicode and ANSI names | MoveFileExW (Unicode) and MoveFileExA (ANSI) |
See Also
CopyFile
DeleteFile
File Management Functions
File Security and Access Rights
GetWindowsDirectory
MoveFileTransacted
MoveFileWithProgress
WritePrivateProfileString
Send comments about this topic to Microsoft
Build date: 12/18/2008