Home Blogs Library Forums Support Advertise
VB Script
 
A Basic Tutorial
File System Programing
 
VBScript and MS Excel
VBScript and MS Access
 
VBScript and MS Word
VBScript and MS Powerpoint
 
Windows Registry Programing
 
Windows Administration
 
Active Directory Programing
 
Miscellaneous Functions
 
You are here...

VBScript - Copying a Folder

The file system object can be used to copy one or more folders and its contents(sub folders Or files) from one location (source location) to another location (destination location). The function CopyFolder can be used to do this. The syntax of the 'CopyFolder' function is given below.

CopyFolder source, destination [, overwrite]

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

The argument 'destination' is the destination folder. This argument cannot have any wild card haracters like '*' or '?'. If the source folder contains any sub folders, it will be created in the destination folder also.

The argument 'overwrite' is an optional boolean argument which takes either 'True' or 'False'. 'True' means overwrite all the files if it file exists in the destination location. 'False' means do NOT overwrite the same file, if it exists in the destination location. If you set the overwrite argument to False, then copying will abort if the script find any file or folder with the same name. The default value is 'True' (Overwrite the same file, if it exists)

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

Dim ObjFso
Dim StrSourceFolder
Dim StrDestinationFolder

StrSourceFolder = "C:\TestFolder1"
StrDestinationFolder = "C:\TestFolder2"

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

'Copying the folder
ObjFso.CopyFile StrSourceFolder, StrDestinationFolder, False

Here if a file with same name exists in the destination location. it will NOT be overwritten. Instead vbscript will produce an error.

Copying a File From Source Location to Destination Location (Same Files Will be Overwritten)

Dim ObjFso
Dim StrSourceFolder
Dim StrDestinationFolder

StrSourceFolder = "C:\TestFolder1"
StrDestinationFolder = "C:\TestFolder2"

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

'Copying the folder
ObjFso.CopyFile StrSourceFolder, StrDestinationFolder

Here if the same folder exists in the destination, it will be overwritten because default value of 'overwrite' argument is 'True'

Copying Multiple Folders from Source Location to Destination Location

Dim ObjFso
Dim StrSourceFolder
Dim StrDestinationFolder

StrSourceFolder = "C:\TestFolder1\*"
StrDestinationFolder = "C:\TestFolder2"

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

'Copying the file
ObjFso.CopyFile StrSourceFolder, StrDestinationFolder, True

This script will copy all files and Folders inside "C:\TestFolder1" to "C:\TestFolder2". Also if any of the file with same name exists in the destination it will be overwritten.