vbscript Moving a File/Folder


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