user396123
user396123

Reputation: 69

renaming .jpg files in a folder using batch file

I have a folder that has bmp files , they may be 4 in a folder or 50 in a folder, but they are

image.bmp
image1.bmp
image2.bmp

I started a batch file with the below code:

@echo off
setlocal enableDelayedExpansion
SET counter=0
SET /P filename=Please enter the filename:
for %%G in (C:\Test_Folder) do (
  ren image*.bmp "%filename%""%counter%".bmp
  SET /A counter=%counter%+1;
  echo "%counter%"
)
pause

but the counter does not increment, can some one give some light to my code?

Upvotes: 0

Views: 463

Answers (1)

Stephan
Stephan

Reputation: 56180

@echo off
setlocal enableDelayedExpansion
SET counter=0
SET /P filename=Please enter the filename:
for %%G in (C:\Test_Folder\image*.bmp) do (
  ren "%%~G" "%filename%!counter!.bmp"
  SET /A counter+=1
  echo "!counter!"
)
pause

Changes:
using delayed expansion for the counter variable.
forprocesses matching files in the folder instead of the folder itself.
use ren to rename single files instead of wildcard usage.
SET /A counter+=1 instead of SET /A counter=!counter!+1 (does the same, but improved readabilty).

Upvotes: 1

Related Questions