0

I have files like

test_abc.html
haha_test_someword.js
haha_morehaha_test_continue_test.someextension

I want to replace all test in a particular folder in recursive way to tommy. So the output would become:

 tommy_abc.html
    haha_tommy_someword.js
    haha_morehaha_tommy_continue_tommy.someextension

Is this possible in command prompt in Windows using ren command?

3 Answers3

2

I do not believe the ren command can do what you want on its own, but a batch script that uses ren can.

I answered a very similar question over on Stack Overflow yesterday. Here is a modified version of that batch script to deal with your problem. It copes with multiple replacements in a single filename:

setlocal ENABLEDELAYEDEXPANSION
set SEARCH_TEXT=test
set REPLACE_TEXT=tommy
for %%A in ("*%SEARCH_TEXT%*") do (
    set OLD_NAME=%%~nxA
    set NEW_NAME=!OLD_NAME:%SEARCH_TEXT%=%REPLACE_TEXT%!
    ren "!OLD_NAME!" "!NEW_NAME!"
)
endlocal

Again, the help text from the SET /? command may be required reading in order to grok this.

wardies
  • 131
1

Use a for loop for recursive renaming. Wildcards for text substitution.

for /r %x in (*test*.*) do ren "%x" "*tommy*.*"

Tip: If running the command once doesn't replace all "test", run the command multiple times or use another for loop.

How to use wildcards in ren

Recursive ren

0

Powershell

gci "C:\files" | ren -n {$_.Name -replace 'test','tommy'}
root
  • 3,920