2

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.

4

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.

  • In the last loop, the delimiter _0 looks like it should result in img_001.jpg becoming photo01.jpg but it actually becomes photo1.jpg; how does it get rid of the middle 0? It works, but why does it work? – TessellatingHeckler Mar 13 '14 at 1:29
  • 1
    @TessellatingHeckler - FOR /F treats any number of consecutive delimiters as a single delimiter. It will split out a token at the first delimiter, and strip out any consecutive delimiters until it reaches the next non-delimiter, where it begins the next token. So "A_B", "A0B", and "A_0_0_0_B" would all yield tokens "A" and "B" if tokens=1* and delims=_0 – dbenham Mar 13 '14 at 2:17
  • I see; delims is a two item list ['_', '0'] instead of a two character string '_0' (I should have remembered that). And it works for img_300.jpg because tokens=1* makes %C = img, %D = the remainder. And the remainder is 300 instead of 300.jpg because %~nB has expanded just the filename without path or extension. Neat! – TessellatingHeckler Mar 15 '14 at 1:33
2

I see you asked for Batch, but others may find PowerShell useful

Get-Childitem "C:\my\folder\" -Recurse -Include *.jpg | ForEach {             
    Rename-Item $_ "$($_.Directory.Name)_$($_.Name -ireplace "img_(0+)", "photo").jpg
}
  • You haven't done the hard bit of renaming the files and numbers from img_00N to photoN. You could use "$($_.Name -ireplace "img_(0+)", "photo").jpg" or "photo$($name.Split('_')[1].TrimStart('0')).jpg" – TessellatingHeckler Mar 13 '14 at 1:42
1

How close do you need to get?

This should do it, but it will keep the 0 in the numbers, e.g. folder1_photo001.jpg instead of folder1_photo1.jpg:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

for /d %%d in (*) do (

    for %%f in ("%%d/img_*.jpg") do (
        set fname=%%f
        set fname=!fname:IMG_=photo!
        echo ren "%%d/%%f" "%%d/%%d_!fname!"
    )
)

(That will print the renaming commands it would run. Remove echo to have it actually run them. Have a backup first, verify you're happy with it, there's no undo, etc.).

I can't think of a nice alternative if you do need 1 instead of 001. Replacing the 00 out would risk changing names where there's a number in the name as well, and counting up would break if the numbers aren't contiguous.

[edit: pinched from another answer, I was assuming the pictures are the only things in the folders. Now looks only for img_*.jpg pattern].

0

Look at some software called Irfanview. It's got a lot of sophisticated bulk rename, resize, conversion, etc. utilities built into it. I think it will do what you want.

0

A much simpler approach. I'm not sure if this works, but try it out .

cd "path of the folder that contains the photos"

for /L %%n in (1 1 %random%) do (

    for %%f in (*.jpg) do (

        rename "%%f" "photo_%%n.jpg"

    )

)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.