1

I have a folder "Main" inside that i have multiple folders "foo","fooxyz" and so on, each folder has around 10-20k files.

I wanted to replace a string in all the files. so basically i had to deal with For /R in "Main", I tried the following code with the help of questions asked but i am not sure what needs to be done exactly.

Setlocal enabledelayedexpansion

Set "Pattern=.jpg"
Set "Replace=_main.jpg"

For /R C:\Main\ %%# in ("*.jpg") Do (
    Set "File=%%~nx#"
    Ren "%%#" "!File:%Pattern%=%Replace%!"
)

If my code can be corrected well and good if not kindly point me in the right direction.

Angad Arora
  • 719
  • 10
  • 25
  • Use `ECHO Ren "%%#" "!File:%Pattern%=%Replace%!"` for debugging purposes and run the script: you would see correct output. You can remove `ECHO` then and run your bulk rename script really. – JosefZ May 12 '16 at 06:48
  • 2
    why not use `ren "%%#" "%%~n#_main%%~x#"` ? – Stephan May 12 '16 at 07:02
  • Josefz - thanks for that, was able to find and solve the problem. – Angad Arora May 12 '16 at 08:55
  • Stephan - i am pretty much a newbie in cmd so i search on stack for answers and modify them as per the requirement, cant code that much in cmd myself :) – Angad Arora May 12 '16 at 08:56
  • Possible duplicate of [search and replace file names - bug fix](http://stackoverflow.com/questions/13702156/search-and-replace-file-names-bug-fix) – aschipfl May 12 '16 at 13:26

0 Answers0