Rename batch script

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
horus117
Posts: 2
Joined: 21 Dec 2016 03:04

Rename batch script

#1 Post by horus117 » 21 Dec 2016 03:18

Hello everyone,
can any good soul help me with small script please?

I want following thing:
to rename jpgs in one directory from something like photo-1, photo-12, photo-54, photo-112 etc. to photo-1, photo-2, photo-3, photo-4 etc in this exact order
now where i see problem is that it mix order during rename operation (it must not take and rename photo-112 before photo-54 etc. which actully happened when i tried something on my own :)

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Rename batch script

#2 Post by Sounak@9434 » 21 Dec 2016 04:02

@horus117
I am assuming that all files are in jpg format and filenames are in photo-%var% where %var% is a variable number. If i got your problem right this script would help.

Code: Select all

@echo off
set ren1=1
set ren2=1
:loop
if exist photo-%ren1%.jpg (
 ren photo-%ren1%.jpg photo-%ren2%.jpg
 set /a ren2+=1
)
set /a ren1+=1
goto loop


EDIT: this script won't close automatically. You manually need to close it.

horus117
Posts: 2
Joined: 21 Dec 2016 03:04

Re: Rename batch script

#3 Post by horus117 » 21 Dec 2016 04:11

Wow, thank you very much sir! Its exactly what i needed.

Sounak@9434
Posts: 100
Joined: 16 Dec 2016 22:31

Re: Rename batch script

#4 Post by Sounak@9434 » 21 Dec 2016 05:33

This script is a bit better. It would close automatically after renaming all your jpg files in current directory.

Code: Select all

@echo off
set ren1=1
set ren2=1
set count1=0
set count2=0
for %%a in (*.jpg) do set /a count1+=1

:loop
if exist photo-%ren1%.jpg (
 ren photo-%ren1%.jpg photo-%ren2%.jpg
 set /a ren2+=1
 set /a count2+=1
 if %count1% equ %count2% exit
)
set /a ren1+=1
goto loop

Post Reply