Search code examples
cmdfile-renamebatch-rename

How to find all directories named DOG and renaming it to CAT using a batch file


In a batch file, I would like to find all the directories under C:\Temp that are literally named DOG. I would then like to loop through the returned results and rename the directories from DOG to CAT. The following code I found lists the directories that are named DOG, however, I am unsure how to manipulate the returned results to rename the directory from DOG to CAT.

for /f "tokens=*" %%G in ('dir /b /s /a:d "C:\Temp\DOG"') do echo Found %%G

My failed attempt

for /f "tokens=*" %%G in ('dir /b /s /a:d "c:\temp\DOG"') do (
  set "source=%%G"
  echo BEFORE
  echo source %source%
  echo.
  set target=%source:\DOG =\CAT%
  echo AFTER
  echo source %source%
  echo target %target%
  ren %source% %target%
)

Solution

  • @echo off
    for /d /r "c:\temp" %%a in (*) do if /i "%%~nxa"=="DOG" ren "%%a" "CAT"