Ace of Hz
Ace of Hz

Reputation: 37

How to rename sequentially selected files with different extensions using a BAT script?

I need a BAT script that renames sequentially selected files with different extensions. Also numbers from 1 to 9 should be padded with two zeroes, and those from 10 to 99 with two zeroes.

It should work like that: I select some files from Windows Explorer, then I call this script from the context menu, then it should ask me the starting number (from where the numbering begins), then it should rename the selected files sequentially.

e.g.

INPUT:
abc.txt
fghdhsk.jpg
khdfyl.jpg
pjdhfjshk.mp3
zhdsghjfj.png

Starting number: 2

OUTPUT:
002.txt
003.jpg
004.jpg
005.mp3
006.png

So far, I found this simpler script, but renames everything (not only the selected files), it doesn't ask for a starting number, and it doesn't add padded zeroes:

@echo off
set /p start=Starting number: 

setlocal enableDelayedExpansion

for /r %%g in (*.*) do (call :RenameIt %%g)
goto :eof
goto :exit

:RenameIt
echo Renaming "%~nx1" to !start!%~x1
ren "%~nx1" !start!%~x1
set /a start+=1
goto :eof

:exit
exit /b

Anyone knows how to modify this script?

Upvotes: 1

Views: 1596

Answers (1)

foxidrive
foxidrive

Reputation: 41224

Test this batch file:

@echo off
set /p "start=Starting number: "
:loop
set "num=00%start%"
set "num=%num:~-3%"
ren "%~1" "%num%%~x1"
set /a start+=1
shift
if not "%~1"=="" goto :loop

Upvotes: 1

Related Questions