-3

I am trying to rename a file from ".bml" extension to ".xml".I am able to rename with below code for one file since i specifying the path and file name.My question is in that folder,there will be more files with different name but will have same ".bml" extension i want rename all the files in that folder. since the below code will be static as i specify filename.Any help is much appreciated

Example as how the files will look in folder :

Test.bml

vbscirpt.bml

uft.bml

Set FSO=Createobject ("Scripting.FileSystemObject")
    strfile="D:\ExportedXml\Test.bml"
    strrename="D:\ExportedXml\Test.xml"
    If FSO.FileExists(strfile) Then
        FSO.MoveFile strfile,strrename
    End If
    Set FSO=nothing

The above code is able to replace Test.bml to Test.xml.

saranya
  • 163
  • 1
  • 10

2 Answers2

0

Use For Each loop to processing files and Regular Expressions to change the format.

Try this way :

Set fso=Createobject("Scripting.FileSystemObject")
Set objRegEx = New RegExp

FolderName = "D:\ExportedXml\"
Set objFolder = fso.GetFolder(FolderName)
Set objFileCol = objFolder.Files
objRegEx.Pattern = "\.bml$"
objRegEx.IgnoreCase = True

For Each objFile In objFileCol
    If objRegEx.Test(objFile.Name) Then
        NewFileName = objRegEx.Replace(objFile.Name, ".xml")
        fso.MoveFile objFile, FolderName & NewFileName
    End If
Next
-1

Although this has been answered, another way of doing this will be to use the power of batch ren command to achieve file extension name change at once.

Set objShell = CreateObject("WSCript.shell")
strPath = "D:\Downloads\"
objShell.Run "cmd /k cd " & strPath & " & ren *.bml *.xml", 0, True
Pankaj Jaju
  • 5,371
  • 2
  • 25
  • 41