jeudi 25 mai 2017

Conditional batch rename (Windows XP)

I have several hundred files whose names have a sequential index at the end:

recipes0001.jpg
recipes0002.jpg
...
recipes0283.jpg

I need to rename them so that the index starts at 976 (not 1) and the leading 0 becomes a '1' when the index reaches 1000

recipes0976.jpg
recipes0977.jpg
...
recipes1000.jpg
recipes1001.jpg
....

I have written this batch code (a modified answer from here):

@echo off
setlocal EnableDelayedExpansion
set i=975
for %%a in (*.jpg) do (
    set /a i+=1
    if i lss 1000 ren "%%a" "recipes0!i!.new"
    if i geq 1000 ren "%%a" "recipes!i!.new"
)
ren *.new *.jpg

It seems that the code always runs the part without the leading 0 since I keep getting recipes976.jpg, recipes1001.jpg etc. What am I doing wrong?

As a follow-up question, can the two if's be combined into one if...else?

Aucun commentaire:

Enregistrer un commentaire