CopyFile Function

Declare Function CopyFile Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long

CopyFile copies a file from one location to another, just like copying a file in Windows Explorer or in some other way. Depending on the value for bFailIfExists, it will either overwrite the target file if it already exists, or will fail. The function retuns 1 if successful, or 0 if an error occured.

lpExistingFileName
The source file; i.e., the file to copy from.
lpNewFileName
The target file; i.e., the new file to create that will be the copy.
bFailIfExists
If 0, the function will overwrite lpNewFileName if it already exists. If non-zero, the function will instead fail.

Example:

' Copy C:\MyStuff\temp.txt to C:\Junk\buffer.txt
' Do not overwrite C:\Junk\buffer.txt if it already exists
x = CopyFile("C:\MyStuff\temp.txt", "C:\Junk\buffer.txt", 1)
If x = 0 Then  ' failure
  Debug.Print "C:\Junk\buffer.txt already exists -- copy failed"
Else
  Debug.Print "Copy successful"
End If

Related Call: MoveFile
Category: Files
Back to the index.


Back to Paul Kuliniewicz's Home Page
E-mail: Borg953@aol.com
This page is at http://members.aol.com/Borg953/api/functions/copyfile.html