简体   繁体   English

使用批处理文件重命名文件夹中的.jpg文件

[英]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 我有一个包含bmp文件的文件夹,它们在一个文件夹中可能是4个,在一个文件夹中可能是50个,但是它们是

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. counter变量使用延迟扩展。
for processes matching files in the folder instead of the folder itself. for匹配文件夹中而不是文件夹本身中文件的进程。
use ren to rename single files instead of wildcard usage. 使用ren重命名单个文件,而不使用通配符。
SET /A counter+=1 instead of SET /A counter=!counter!+1 (does the same, but improved readabilty). SET /A counter+=1而不是SET /A counter=!counter!+1 (功能相同,但提高了可读性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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