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 - Moving a File (Also for Renaming the file)

The file system object can be used to move one or more files from one location (source location) to another location (destination location). Moving a file from one location to another location is equivalent to Cut the file from one location and Pasting it in another location. The function MoveFile can be used to do this. The syntax of the 'MoveFile' function is given below.

MoveFile source, destination

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

The argument 'destination' should be complete path to the destination location. It must end with a backward slash (\) or new filename. If the destination is ending with a backslash(\), then it is assumed that the destination folder exists and the file will be moved to this location with the same name. If the destination is ending with a new filename, then the file will be moved to the destination with the new filename. This argument cannot have wild card characters ('*' or '?')

Moving a File From Source Location to Destination Location

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")

'Moving the file
ObjFso.MoveFile StrSourceLocation & "\" & StrSourceFileName, StrDestinationLocation & "\" & StrDestinationFileName

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

Renaming a File

The MoveFile function also can be used for renaming an existing file. To do this give the destination location same as the source location. Give the destination filename as the new filename.

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.