How can I search and replace specific text in a filename, for multiple files, with different extensions?

Solution for How can I search and replace specific text in a filename, for multiple files, with different extensions?
is Given Below:

I have a folder which contains files of varying extensions (ie .xls, .xlsx, .txt, .csv).
Each one of these files contain a string of text in the name which is the same.

  • SO_TESTSOEntry.xls
  • PurchasingTEST.xlsx
  • TESTLog.txt
  • XTESTUpload.csv

The goal here is to replace all the “TEST” strings with “LIVE

I’ve seen posts online which refer to search and replace, within a text file but I’m having a hard time finding one which does this for file names, in bulk.

I have the code below which I was hoping could do the trick, but it’s just for file extensions and something tells me that some slight modifications might be needed in order to make it work. I just don’t know what:

This was the code before (which only renames the extensions):

forfiles /P D:CompanyDataTEST /S /M *.csv /C "cmd /c rename @file @fname.TXT"

When I tried to modify it to look for a specific string it doesn’t work:

forfiles /P D:CompanyDataTEST /S /M *TEST.* /C "cmd /c rename @file @fname*LIVE.*"

You can use string substitution to change the name of the files in the current directory:

@echo off

setlocal enabledelayedexpansion

for %%a in (*) do (
    set old_fn=%%a
    set new_fn=!old_fn:TEST=LIVE!
    if not "!old_fn!" == "!new_fn!" (
        echo "!old_fn!" to "!new_fn!"
        ren "!old_fn!" "!new_fn!"
    )
)