Copying a File in VB Script

The file system object can be used to copy one or more files from one location (source location) to another location (destination location). The function CopyFile can be used to do this. The syntax of the 'CopyFile' function is given below.

CopyFile source, destination [, overwrite]

The argument 'source' is the source file name with the complete path. This argument can contain wild card characters ('*' or '?') also for copying multiple files.

The argument 'destination' can be i. The destination file name along with the complete path or ii. The path to the destination locacation ending with a backward slash (\). This argument cannot have wild card characters ('*' or '?')

The argument 'overwrite' is an optional boolean argument which takes either 'True' or 'False'. 'True' means overwrite if the same file exists in the destination location. 'False' means do NOT overwrite the same file, if it exists in the destination location. The default value is 'True' (Overwrite the same file, if it exists)

Copying a File From Source Location to Destination Location (Do NOT Overwrite, If it Exists in the Destination) 

Dim ObjFso
Dim StrSourceLocation
Dim StrDestinationLocation
Dim StrSourceFileName
Dim StrDestinationFileName

StrSourceLocation = "C:\TestFolder1"
StrDestinationLocation = "C:\TestFolder2"
StrSourceFileName = "Source File.txt"
StrDestinationFileName = "Destination File.txt"

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFile StrSourceLocation & "\" & StrSourceFileName, StrDestinationLocation & "\" & StrDestinationFileName, False

Copying a File From Source Location to Destination Location (With the Same File Name)

Dim ObjFso
Dim StrSourceLocation
Dim StrDestinationLocation
Dim StrSourceFileName
Dim StrDestinationFileName

StrSourceLocation = "C:\TestFolder1"
StrDestinationLocation = "C:\TestFolder2"
StrSourceFileName = "Source File.txt"
StrDestinationFileName = "Destination File.txt"

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFile StrSourceLocation & "\" & StrSourceFileName, StrDestinationLocation & "\"

Here notice that the destination location is ending with a backward slash (\). This is very important, otherwise you will get a permission denied error. (You will be trying to replace a directory file with a text file which will be prevented by the OS). If the same file exists in the destination, it will be overwritten because default value of 'overwrite' argument is 'True'

Copying Multiple Files from Source Location to Destination Location

Dim ObjFso
Dim StrSourceLocation
Dim StrDestinationLocation
Dim StrSourceFileName
Dim StrDestinationFileName

StrSourceLocation = "C:\TestFolder1"
StrDestinationLocation = "C:\TestFolder2"

'All text files will be copied to destination
StrSourceFileName = "*.txt"

'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")

'Copying the file
ObjFso.CopyFile StrSourceLocation & "\" & StrSourceFileName, StrDestinationLocation & "\" , True

This script will copy all files with extention '.txt' from source location to destination location. '*' is a wild card character that can stand for any character, any number of times. Also if any of the file with same name exists in the destination it will be overwritten.

Powered by Bullraider.com