0

I have, say, ten images that I'd like to rename in a looping fashion every night. I've written a batch file that does the following:

@echo off
cls
ren image10.jpg imagetemp.jpg
ren image1.jpg image10.jpg
ren image2.jpg image1.jpg
ren image3.jpg image2.jpg
ren image4.jpg image3.jpg
ren image5.jpg image4.jpg
ren image6.jpg image5.jpg
ren image7.jpg image6.jpg
ren image8.jpg image7.jpg
ren image9.jpg image8.jpg
ren imagetemp.jpg image9.jpg
exit

I'd like to rewrite this to be a loop since my image count just increased, however I'm having some issues - my script just terminates and the shell Window closes without letting me see any of the output (I had changed all "rens" to "echos" to see what the output would be). Here's my current attempt:

cls
ren image59.jpg imagetemp.jpg
ren image1.jpg image59.jpg

FOR %a IN (2 59) DO (
    set t = %a% - 1
    ren image%a%.jpg image%t%.jpg
)
ren imagetemp.jpg image58.jpg
exit

As I said before, I get either "a is unexpected", or when I try and change it to %%a, it just crashes and the window closes. Any tips?

4
  • 1
    Try putting in a pause to see what the errors are? Also try using %%a rather than %a%. Commented Jan 18, 2012 at 16:50
  • Thanks... using %%a works, but I still can't set or access t. Commented Jan 18, 2012 at 17:09
  • I have just run a little experiment in cmd, t isn't working because batch can't handle numbers. If you set a to 259 thats fine, if you set t to a - 1 it will echo as "259 - 1", not 258. Also when using set it only seems to work when you don't have spaces between the equals sign, so it would only work like set t=%a% - 1 Commented Jan 18, 2012 at 17:19
  • @Bali C To handle numbers in batch-files you should use /A. Commented Jan 18, 2012 at 17:41

1 Answer 1

3

Shifting all the images by one, the first one is moved back to the very last position:

@echo off
cls
set count=59
ren image1.jpg imagetemp.jpg

for /L %%a IN (2,1,%count%) do call :rename %%a

ren imagetemp.jpg image%count%.jpg
goto :EOF

:rename
set /A t=%1-1
ren image%1.jpg image%t%.jpg  

Little enhancement added: set count to the number of images you are going to cycle through.

Sign up to request clarification or add additional context in comments.

3 Comments

better than what I was about to make up =D
You're a better man/woman than I! Cheers. This DOS syntax is freaky stuff.
@Darrrrrren it's cmd syntax, not DOS. There's no set /a or for /l in DOS. DOS and Windows cmd are completely different things

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.