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.
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.