How to Rename Files With a VBS
Renaming files is a common practice with all development and programming tools. When saving a file it is usually a good idea to rename it. The renamed file will often carry a BAK extension, or have the date as part of the new file name. Whatever the reason, knowing how to rename a file is essential in VBS (or VBScript, which stands for Visual Basic Script) or any other programming language. To complete this task, you'll need to have VBS programming experience.
Instructions
-
-
1
Open the text editor you are using to write the code.
-
2
Locate the area in your code where you are going to add the line to rename the file.
-
-
3
Type in the following line of code:
Rename ("FilePath\FileName")
The FilePath is the full path to the file (i.e. "C:\windows\temp").
The FileName is the new name of the file (i.e. "MyNewDoc.txt").So, the code would look like this:
Rename ("C:\windows\temp \ MyNewDoc.txt")
-
1
Tips & Warnings
This article is intended for those with programming experience in VBScript.
A piece of sample code would look like the following:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Name = " & "'c:\\windows\\temp\\MyOldDoc.txt")
errResult = objFile.Rename("C:\windows\temp \ MyNewDoc.txt")
The full path of the file must be used or an error will result.