vbscripthp-uft

How to rename different named file with same extension in vbscript


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.


Solution

  • 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