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 same function can be also used for renaming files. 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 '?')
Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim SourceFileName
Dim DestinationFileName
SourceLocation = "C:\TestFolder1"
DestinationLocation = "C:\TestFolder2"
SourceFileName = "Source File.txt"
DestinationFileName = "Destination File.txt"
'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Moving the file
ObjFso.MoveFile SourceLocation & "\" & SourceFileName, DestinationLocation & "\" & DestinationFileName
Here if a file with same name exists in the destination location. it will NOT be overwritten. Instead vbscript will produce an error.
There is no default overwriting functionality available in the "MoveFile" function. So what you have to do is, check whether source file exists in the destination folder; if it exists, delete it. Check out other articles in this section to find out how to check existence of a File or Folder and how to delete file or folder.
Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim FileName
SourceLocation = "C:\TestFolder1"
DestinationLocation = "C:\TestFolder2"
FileName = "Source File.txt"
'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Moving the file
ObjFso.MoveFile SourceLocation & "\" & FileName, DestinationLocation & "\"
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'
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 SourceLocation
Dim DestinationLocation
Dim SourceFileName
Dim DestinationFileName
SourceLocation = "C:\TestFolder1"
DestinationLocation = "C:\TestFolder1"
SourceFileName = "Original File Name.txt"
DestinationFileName = "New File Name.txt"
'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Moving the file
ObjFso.MoveFile SourceLocation & "\" & SourceFileName, DestinationLocation & "\" & DestinationFileName
To move multiple files from source location, you have to provide wild cards in the source file name and the destination location should end with a \ . For example
file-?.txt will move file-1.txt, file-2.txt etc
*.txt will move all files with an extension .txt
* will move all files
Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim SourceFileName
Dim DestinationFileName
SourceLocation = "C:\TestFolder1"
DestinationLocation = "C:\TestFolder2"
'All text files will be copied to destination
SourceFileName = "*.txt"
'Creating the file system object
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Copying the file
ObjFso.MoveFile SourceLocation & "\" & SourceFileName, DestinationLocation & "\"
This script will move 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.