Copies an existing file to a new file.
The
CopyFileEx function provides two additional capabilities.
CopyFileEx can call a specified callback function each time a portion of the copy operation is completed, and
CopyFileEx can be canceled during the copy operation.
To perform this operation as a transacted operation, use the CopyFileTransacted function.
Syntax
BOOL WINAPI CopyFile(
__in LPCTSTR lpExistingFileName,
__in LPCTSTR lpNewFileName,
__in BOOL bFailIfExists
);
Parameters
- lpExistingFileName [in]
-
The name of an existing file.
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.
If lpExistingFileName does not exist, CopyFile fails, and GetLastError returns ERROR_FILE_NOT_FOUND.
- lpNewFileName [in]
-
The name of the new file.
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.
- bFailIfExists [in]
-
If this parameter is TRUE and the new file specified by lpNewFileName already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.
Return Value
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call
GetLastError.
Remarks
Security attributes for the existing file are not copied to the new file. To copy security attributes, use the SHFileOperation function.
File attributes for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to
CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute. For more information, see
Retrieving and Changing File Attributes.
This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY attribute set.
When
CopyFile is used to copy an encrypted file, it attempts to encrypt the destination file with the keys used in the encryption of the source file. If this cannot be done, this function attempts to encrypt the destination file with default keys, as in Windows 2000. If neither of these methods can be done,
CopyFile fails with an ERROR_ENCRYPTION_FAILED error code.
Windows 2000: When
CopyFile is used to copy an encrypted file, the function attempts to encrypt the destination file with the default keys. No attempt is made to encrypt the destination file with the keys used in the encryption of the source file. If it cannot be encrypted,
CopyFile completes the copy operation without encrypting the destination file.
Symbolic link behavior—If the source file is a symbolic link, the actual file copied is the target of the symbolic link.
If the destination file already exists and is a symbolic link, the symbolic link is overwritten by the source file.
Examples
For an example, see
Retrieving and Changing File Attributes.
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 | CopyFileW (Unicode) and CopyFileA (ANSI) |
See Also
- CopyFileEx
- CopyFileTransacted
- CreateFile
- File Management Functions
- MoveFile
- Symbolic Links
Send comments about this topic to Microsoft
Build date: 6/25/2009