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
Dtynin3w 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
Sterling3w ago
Okay, okay... Thanks very much @Dtynin
Enthernet Code
@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
Sterling3w ago
Alright, I will take note of that. Thanks @Enthernet Code
Marvee Amasi
Marvee Amasi3w 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_ali3w 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
More Posts
Understanding Anonymous Inode 0 Entries in /proc/pid/maps for Memory Usage in Embedded LinuxI'm investigating memory usage in my embedded Linux application using `/proc/pid/maps`. I'm confusedHow to Correctly Calibrate a Load Cell Using HX711 Library for Accurate Weight Measurements?Hello everyone, I am trying to calibrate a load cell to obtain the correct weight. I followed the inDIY haptic SmartKnob: BLDC motor + round LCDThis innovative input device, SmartKnob, is an **open-source** input device with software-configurabHow to Implement OTA Updates for ESP32 with FreeRTOS on Google Cloud IoT Core?@Middleware & OS Hello guys am interested in `over the air` (OTA) firmware update of an ESP32 microHow to Identify and Resolve Latency Spikes in a Linux GPIO Application Using Preempt_rt Kernel PatchHi guys, during a discussion yesterday, my colleague and I were exploring ways to detect and solve lAre there alternatives to system() that might be more performant for CGI scripts on embedded Linux?I'm facing a performance issue with the system() call in C on my embedded Linux system. The command What exactly do inner and outer cache policies refer to?Hey guys @Middleware & OS I am trying to develop my own RTOS kernel for a Cortex-M7 STM32F746 DiscoDeveloping a smart thermostat device using Android Things OS@Middleware & OS Hey guys, I'm developing a smart thermostat device using Android Things OS. I woulDeveloping automotive test systems with STM32 Nucleo devices (like Nucleo144) using RTOSI've been working with STM32 Nucleo boards (mainly Nucleo144) for basic projects using CubeIDE. Now How to Keep Spring Boot App Running After Closing PuTTY Session Without Using a Linux Service?Hi Everyone, I'm running a Spring Boot application with an embedded Tomcat server deployed as an exe