简体   繁体   中英

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?

@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.
for processes 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM