Fixing 'Missing Operator' Error in Batch Script for Renaming JPG Files on Windows

I'm trying to rename image files (JPGs) on a Windows machine. I want to add an incrementing number (e.g., file1.jpg, file2.jpg, etc.) to each filename. I wrote a batch script using a loop, but I'm encountering a 'Missing operator' error. Here's the relevant part of the script:
setlocal EnableDelayedExpansion
set filename=file
set counter=0
for /f "usebackq delims=*" %i in (`dir /b *.jpg`) do (
set /a counter+=1
ren "%i" "%filename%!counter!.jpg"
)
setlocal EnableDelayedExpansion
set filename=file
set counter=0
for /f "usebackq delims=*" %i in (`dir /b *.jpg`) do (
set /a counter+=1
ren "%i" "%filename%!counter!.jpg"
)
Can anyone help identify the issue causing the error? @Middleware & OS
6 Replies
Dtynin
Dtynin6mo ago
In batch scripting use double exclamation marks as in !! to expand your variables within arithmetic operations. Lemme give you a fix:
ren "%i" "%filename%%counter!!%.jpg"
ren "%i" "%filename%%counter!!%.jpg"
It's just syntax error @Sterling
Sterling
Sterling6mo ago
Okay, okay... Thanks very much @Dtynin
Enthernet Code
Enthernet Code6mo ago
@Sterling The issue causing the Missing operator error is that when you're running the bash script in a command prompt window, you need to use double percent signs %% for the loop variable %i.
Sterling
Sterling6mo ago
Alright, I will take note of that. Thanks @Enthernet Code
Marvee Amasi
Marvee Amasi6mo ago
Yh something like this
setlocal EnableDelayedExpansion
set filename=file
set counter=0
for /f "usebackq delims=*" %%i in (`dir /b *.jpg`) do (
set /a counter+=1
ren "%%i" "%filename%!counter!.jpg"
)
setlocal EnableDelayedExpansion
set filename=file
set counter=0
for /f "usebackq delims=*" %%i in (`dir /b *.jpg`) do (
set /a counter+=1
ren "%%i" "%filename%!counter!.jpg"
)
youcef_ali
youcef_ali6mo ago
Select all the file you name in explorer put the header you want, it will be incremented auto
Want results from more Discord servers?
Add your server