Make your own free website on

      API Tip

CopyFile API Function



      CopyFile API function is very similar to DeleteFile function i showed to You couple weeks ago. You can use this function to copy files from one location to another fast and easy.

The declaration of CopyFile:

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

All this code above has to be declared in one line. lpExistingFileName has to have a string holding a path to an existing file you want to copy, lpNewFileName string has to contain the new location of the file together with the filename, and bFailIfExists controls how the function will if the file you want to copy exists in that new location. If bFailIfExists is set to true and the file exists, the function will fail, if it is set to false, the function will overwrite the existing file. 
If function succeeds, the return value is non-zero.

NOTE: strings used in this API cannot exceed MAX_PATH length (which is 256 for most win os). And remember, not only specify the path where to copy the file, but also provide the file name of the copied file.

Example: Copies test.txt in c:\ to d:\test\ folder:

                Private Sub cmdDelete_Click()

                   Dim rc As Long 'for checking functions results

                        rc = CopyFile("c:\test.txt", "d:\test\test.txt", False)

                       If rc <> 0 Then MsgBox "Success!"

                End Sub

Api Tips - Home



Made By Laimonas Simutis. 2001.