Copying and Renaming file in VBScript Options

davegate Posts: 182 Points: 894
Posted: Thursday, January 21, 2016 10:52:19 AM

In VBScript, you can use FileSystemObject.CopyFile method to copy and/or rename a file.

Dim fso      ' FileSystemObject object.
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists("c:\temp\anyfile.html") Then
   fso.CopyFile "c:\temp\anyfile.html", "c:\destfolder\newfile.html",true
End If

This method lets us copy one or more files from one location ( the source) to another (destination). Wildcards can be used within the source string, providing it is the last component in the path, to enable the copying of multiple files, but cannot be used in the destination string.

The third parameter is a Boolean. True allows the overwriting of existing files in the destination, providing that the permissions on the destination allow it.

It is recommended that you use the FileExists method when copying or moving a file - if a source file doesn't exist you'll get an error.

Users browsing this topic