Search code examples
batch-filedos

batch renaming multiple file names


I'd like to rename specific image names from *_PolishedChrome.jpg to be *-pch.jpg. I tried a bunch of different ways, included the rename DOS command, but cannot find the right way to do it. The files are in a folder on my desktop and they are all .jpg images. There are multiple files with the last part of the image name.

Example: rename 3-04012C_PolishedChrome.jpg to be 3-04012C-pch.jpg

I'd appreciate any help, thanks!


Solution

  • I think this will do what you are looking for (just place it in the same folder as the jpgs and run):

    Remember to make a backup of your files before running this, as it does move files on the disk. It could be done with rename, but that is more effort, as I'd have to parse out the file name.

    Also note that running this twice will re-rename your files, so it can only really be run once within a folder... that can be changed, but this is more of a starting point.

    @echo off
    setlocal ENABLEDELAYEDEXPANSION
    for /r %%i in (*.jpg) do (
    set str=%%i
    set str=!str:_PolishedChrome.jpg=!-pch.jpg!!
    move %%i !str!
    )
    

    DEBUG version - prints out a log.txt to see what went wrong.

    @echo off 
    setlocal ENABLEDELAYEDEXPANSION
    for /r %%i in (*.jpg) do (
    set str=%%i
    set str=!str:_PolishedChrome.jpg=!-pch.jpg!!
    move %%i !str!
    @echo move %%i !str! >> log.txt
    )