CopyFile Function

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

Platforms: Win 32s, Win 95/98, Win NT

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 the file C:\MyStuff\temp.txt to C:\Junk\buffer.txt.
' Do not overwrite C:\Junk\buffer.txt if it already exists.
Dim retval As Long  ' return value

' copy the file
retval = CopyFile("C:\MyStuff\temp.txt", "C:\Junk\buffer.txt", 1)
If retval = 0 Then  ' failure
  Debug.Print "Copy failed -- C:\Junk\buffer.txt already exists.
Else  ' success
  Debug.Print "Copy succeeded."
End If

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


Go to Paul Kuliniewicz's Home Page.
E-mail: rogue953@hotmail.com
This page is at http://skyscraper.fortunecity.com/transmission/45/api/c/copyfile.html