中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-05 18:36
48,039 topics / 350,124 posts / today 2 new / 48,251 members
DOS批处理 & 脚本技术(批处理室) » How to change the file names of the same type in the same directory into consecutive digital names?
Printable Version  1,424 / 14
Floor1 fanglor Posted 2007-04-13 14:17
初级用户 Posts 30 Credits 68
Just now I saw a brother asking for help to change file names with batch processing, and it reminded me of an idea I had a long time ago.

For example, there are N JPG files in a directory, how to change them to 1.JPG, 2.JPG, 3.JPG and so on.

I have tried many times without success. I hope some kind expert can help solve it. It's best to attach comments to the code. I have limited ability and may not understand it. Thank you.
Floor2 bjsh Posted 2007-04-13 21:18
银牌会员 Posts 621 Credits 2,000
Let's try this to see if it meets the requirements


  1. @echo off
  2. set count=1
  3. for /f "delims=" %%a in ('dir /b *.jpg') do ren %%a %count%.jpg && set /a count+=1
BJSH posted on: 2007-04-13 08:07


[ Last edited by bjsh on 2007-4-13 at 09:17 AM ]
Floor3 ccwan Posted 2007-04-13 21:55
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
```@echo off
set num=0
for /f "delims=" %%i in ('dir/b *.jpg') do set/a num+=1&call :re "%%i"
goto :eof
:re
ren %1 %num%.jpg
```
Floor4 htysm Posted 2007-04-13 23:09
高级用户 Posts 415 Credits 866
Does this require a batch process? Manual work is faster. Edit > Select All > Rename > Enter 1.jpg. Press Enter.
Floor5 zh159 Posted 2007-04-13 23:29
金牌会员 Posts 1,467 Credits 3,687
Originally posted by htysm at 2007-4-13 10:09:
Does this require a batch processing? Doing it manually is faster.
Edit > Select All > Rename > Enter 1.jpg. Press Enter.

Unfortunately, the first one is 1.jpg, and the others are 1 (n).jpg (XP SP2)
Floor6 ccwan Posted 2007-04-14 00:03
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
My Windows 2000 can't achieve what Brother htysm said
Floor7 fanglor Posted 2007-04-14 03:59
初级用户 Posts 30 Credits 68
Thanks to the help of all brothers!

After testing: The method on the second floor seems to have a problem. Error message: There is a renamed file or the file is not found. Result: Only one can be changed to 1.JPG!

The third floor is okay. Hehe... I didn't expect that the problem that has troubled me for many years was finally solved today.
Floor8 bjsh Posted 2007-04-14 04:26
银牌会员 Posts 621 Credits 2,000
Yes; mine should enable variable delay; after correction it is as follows


  1. @echo off & setlocal enabledelayedexpansion
  2. set count=1
  3. for /f "delims=" %%a in ('dir /b *.jpg') do ren "%%a" "!count!.jpg" && set /a count+=1
BJSH posted on: 2007-04-13 15:15
Floor9 fanglor Posted 2007-04-15 03:20
初级用户 Posts 30 Credits 68
### 关于 `for /f "delims=" %%a in ('dir /b *.jpg') do ren "%%a" "!count!.jpg" && set /a count+=1"` 的问题解释

#### 1. 关于 `"delims="` 的意思
在 `for /f` 循环中,`delims` 是用于指定分隔符的参数。默认情况下,`for /f` 会以空格、制表符等作为默认的分隔符来分割每行文本。而 `delims=` 表示将分隔符设置为空,这样就会按整行来处理,不会对每行进行分割,而是将每行作为一个整体赋值给循环变量 `%%a`。

#### 2. 关于 `"!count!.jpg"` 能否调用后面的 `count`
这里使用了延迟环境变量扩展。在批处理中,普通的变量 `%count%` 在循环内部如果没有开启延迟扩展的话,获取的是循环初始时的 `count` 值,而使用 `!count!` 并且开启了延迟环境变量扩展(一般通过 `setlocal enabledelayedexpansion` 来开启),就可以在循环过程中获取到 `count` 实时变化后的值,所以这里 `!count!.jpg` 能够调用到后面实时更新的 `count` 值。

不过你提供的内容中前面应该还有开启延迟环境变量扩展的相关代码,比如通常会有 `@echo off` 之后跟着 `setlocal enabledelayedexpansion` 之类的代码来支持延迟环境变量扩展,否则单独这一句可能无法正常获取到实时更新的 `count` 值。
Floor10 kich Posted 2007-04-15 04:12
中级用户 Posts 168 Credits 397
No need to use batch processing!!
ACDsee 9 (software) can support batch renaming!!
It's just that you are researching technology, you can look at what they say. If it's just batch renaming file names, then you can use this software, which is very suitable for renaming photos taken by digital cameras!!
Floor11 Waterlive Posted 2007-05-28 13:28
初级用户 Posts 21 Credits 52
Fellow friends on the 4th and 9th floors, we come here to learn batch processing, and using other tools to achieve the same effect goes against the purpose of learning.

I've been thinking about how to write a batch rename these days... Got inspired!
Floor12 zh159 Posted 2007-05-28 13:45
金牌会员 Posts 1,467 Credits 3,687
```
@echo off
for /f "delims=" %%a in ('dir /b *.jpg') do set /a count+=1&&call ren "%%a" "%%count%%.jpg"
```
Floor13 youxi01 Posted 2007-05-28 14:11
高级用户 Posts 247 Credits 846 From 湖南==》广东
Further "simplify":
@echo off
for %%a in (*.jpg) do set /a count+=1&&call ren "%%a" "%%count%%.jpg"
Floor14 qq82015930 Posted 2007-05-28 21:19
中级用户 Posts 109 Credits 235
Got another harvest. Thanks
Floor15 liu3157551 Posted 2007-09-03 09:54
中级用户 Posts 164 Credits 259
Good stuff, included ^_^ Monday, September 3, 2007 9:44:54
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023