vbscript FileSystem Objects Moving a File/Folder

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Methods Used:

.MoveFile(Source, Dest)
.MoveFolder(Source, Dest)

The following code illustrates the use of MoveFile method to Move a file to a new location. The same thing can be achieved for the folders by using the MoveFolder method.

Code:

Dim objFso, strSourcePath, strDestPath
strSourcePath = "C:\Users\GS\Desktop\Source.txt"
strDestPath = "C:\Users\GS\Desktop\Folder\Dest.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strSourcePath) then
    objFso.MoveFile strSourcePath, strDestPath
End If
Set objFso = Nothing

NOTE: We do not have any method of a filesystem object which allows us to rename a file. However, this can be achieved by MoveFile method by moving the file to the same location with a different name as shown below:

Dim objFso, strSourcePath, strDestPath
strSourcePath = "C:\Users\GS\Desktop\OldName.txt"
strDestPath = "C:\Users\GS\Desktop\NewName.txt"       'Location is same but the name is different
Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(strSourcePath) then
    objFso.MoveFile strSourcePath, strDestPath
End If
Set objFso = Nothing


Got any vbscript Question?