CopyFile Method

Copies one or more files from one location to another.

Syntax

object.CopyFile (source, destination)

Arguments

  • Object: Required. Any object variable returning a FileSystemObject object.
  • Source: Required. The path and name of the file to be copied. The filename (but not the path) can contain wildcard characters.
  • Destination: Required. The path of the destination folder where copy of file must be made. Destination cannot include wildcard characters.

The following example illustrates the use of the CopyFile method:

Example 1:

Function CopySingleFile(source, destination)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile source, destination
End Function

'Usage
CopySingleFile "C:\Temp\test.txt", "C:\Temp\dest\"

'Note – If Source file doesn’t exist, an empty file will be created at 
destination

Example 2:

Function CopyMultipleFiles(source, destination)
    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.CopyFile source, destination
End Function

' Usage
CopyMultipleFiles "C:\Temp\*.txt", "C:\Temp\Destination\"