0

I am very new to VB Script. I am trying to rename a file through VB Script could any one please help me in this ?

I just tried this but didn't work.

Dim OldFile As String
Dim NewFile As String

OldFile = "C:\apache-tomcat-8.0.44\apache-tomcat-8.0.44\webapps\" & "\" & timeStampDir & "\" & "output_11.docx"

NewFile = "C:\apache-tomcat-8.0.44\apache-tomcat-8.0.44\webapps\" & "\" & timeStampDir & "\" & "output.docx"
        Name OldFile As NewFile
Rajesh Gurbani
  • 211
  • 1
  • 6
  • 18

2 Answers2

3

Use FileSystemObject and use Name property of a object referring the file you want to rename.

Dim OldFile, FSO, objFile
Set FSO = WScript.CreateObject("Scripting.FileSystemObject")

OldFile = "C:\apache-tomcat-8.0.44\apache-tomcat-8.0.44\webapps\" + CStr(timeStampDir) + "\output_11.docx"

Set objFile = FSO.GetFile(OldFile)
objFile.Name = "output.docx"

Keep in mind that you need to correctly escape all of the whitespaces in your oldFile variable, otherwise the file may not be found.

GTAVLover
  • 1,407
  • 3
  • 22
  • 41
-1
Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.MoveFile OldFile, NewFile

Set objFSo = Nothing
Mithilesh Indurkar
  • 481
  • 1
  • 5
  • 12