Microsoft Docs

CopyFile function

The CopyFile function copies an existing file to a new location.

This function is part of the Win32 API.

Syntax


BOOL CopyFile(
  LPCTSTR lpExistingFileName,
  LPCTSTR lpNewFileName,
  BOOL    bFailIfExists
);
                

Parameters

  • lpExistingFileName

    The name of an existing file.

  • lpNewFileName

    The name of the new file.

  • bFailIfExists

    Specifies whether the function should fail if the new file already exists.

    • If this parameter is TRUE and the new file exists, the function fails.
    • If this parameter is FALSE and the new file exists, the function overwrites the existing file.

Return Value

BOOL
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

If the destination file exists and the bFailIfExists parameter is FALSE, CopyFile overwrites the destination file. If the destination file exists and bFailIfExists is TRUE, the function returns FALSE and GetLastError returns ERROR_FILE_EXISTS.

This function does not support copying directories. Use SHCopyDirectoryEx or similar functions for directory copying.

For more advanced copy operations, such as copying with progress indicators or specific flags, consider using CopyFileEx.

Requirements

Client: Windows XP and later
Server: Windows Server 2003 and later
Header: winbase.h (include windows.h)
Library: Use Kernel32.lib

See Also