Windows – Renaming multiple files inside a folder using Windows batch commands

batchrenamewindows

This is taking the post located here and spinning things up a bit.
As typical, I'm trying to rename several images inside a folder to be the name of the folder they're in and then add the suffix "photo1", "photo2" etc.

In other words, I'm trying to go from here:

Folder1
  IMG_001.jpg
  IMG_001.jpg
  IMG_003.jpg

To here:

Folder1
  Folder1_photo1.jpg
  Folder1_photo2.jpg
  Folder1_photo3.jpg

Catch is there several hundred of these "Folder" folders, and each one of these will need to have the photos inside of it renamed.

I know there is 3rd party software out there that can do this but I'm looking for a way to run this as a Windows .bat.

If anybody has an idea please share. Thanks for your time.

Best Answer

Assuming all folders are in the same root folder, and all images match the template IMG_n.jpg, then the following one liner should work on the command line:

for /d %A in ("yourRootPath\*") do @for %B in ("%A\img_*.jpg") do @for /f "tokens=1* delims=_0" %C in ("%~nB") do ren "%B" "%~nxA_photo%D.jpg"

Don't forget to double up the percents if you use the command in a batch script.

Related Question