Rename a file using VBScript

I am trying to rename a file and used the code below, but it does not seem to work. Can someone please tell me why? I use this stream for reference Rename files without copying in one folder

FSO.GetFile ("MyFile.txt") .Name = "Hello.txt"

+9
vbscript
source share
5 answers

You can rename the file using FSO by moving it: MoveFile Method .

Dim Fso Set Fso = WScript.CreateObject("Scripting.FileSystemObject") Fso.MoveFile "A.txt", "B.txt" 
+24
source share

I see only one reason why your code is not working, a missing quote after the file name line:

VBScript:

 FSO.GetFile("MyFile.txt[missed_quote_here]).Name = "Hello.txt" 
+10
source share

Yes you can do it.
Here I rename the .exe file to the .txt file

rename file

 Dim objFso Set objFso= CreateObject("Scripting.FileSystemObject") objFso.MoveFile "D:\testvbs\autorun.exe", "D:\testvbs\autorun.txt" 
+1
source share
 Rename filename by searching the last character of name. For example, Original Filename: TestFile.txt_001 Begin Character need to be removed: _ Result: TestFile.txt Option Explicit Dim oWSH Dim vbsInterpreter Dim arg1 'As String Dim arg2 'As String Dim newFilename 'As string Set oWSH = CreateObject("WScript.Shell") vbsInterpreter = "cscript.exe" ForceConsole() arg1 = WScript.Arguments(0) arg2 = WScript.Arguments(1) WScript.StdOut.WriteLine "This is a test script." Dim result result = InstrRev(arg1, arg2, -1) If result > 0 then newFilename = Mid(arg1, 1, result - 1) Dim Fso Set Fso = WScript.CreateObject("Scripting.FileSystemObject") Fso.MoveFile arg1, newFilename WScript.StdOut.WriteLine newFilename End If Function ForceConsole() If InStr(LCase(WScript.FullName), vbsInterpreter) = 0 Then oWSH.Run vbsInterpreter & " //NoLogo " & Chr(34) & WScript.ScriptFullName & Chr(34) WScript.Quit End If End Function 
0
source share

Below code absolutely worked for me to update the file extension.

Example: abc.pdf to abc.txt

 Filepath = "Pls mention your Filepath" Set objFso = CreateObject("Scripting.FileSystemObject") '' Below line of code is to get the object for Folder where list of files are located Set objFolder = objFso.GetFolder(Filepath) '' Below line of code used to get the collection object to hold list of files located in the Filepath. Set FileCollection = objFolder.Files For Each file In FileCollection WScript.Echo "File name ->" + file.Name ''Instr used to Return the position of the first occurrence of "." within the File name s = InStr(1, file.Name, ".",1) WScript.Echo s WScript.Echo "Extn --> " + Mid(file.Name, s, Len(file.Name)) 'Left(file.Name,s-1) = Used to fetch the file name without extension ' Move method is used to move the file in the Desitnation folder you mentioned file.Move(Filepath & Left(file.Name,s-1)&".txt") Next 
-one
source share

All Articles