China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-05 17:57
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to change the file names of the same type in the same directory into consecutive digital names? View 1,423 Replies 14
Original Poster Posted 2007-04-13 14:17 ·  中国 湖北 武汉 电信
初级用户
Credits 68
Posts 30
Joined 2007-01-16 07:05
19-year member
UID 76659
Gender Male
Status Offline
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.
Floor 2 Posted 2007-04-13 21:18 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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 ]
Floor 3 Posted 2007-04-13 21:55 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
```@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
```
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 4 Posted 2007-04-13 23:09 ·  中国 安徽 芜湖 电信
高级用户
★★★
Credits 866
Posts 415
Joined 2005-12-04 11:19
20-year member
UID 46459
Status Offline
Does this require a batch process? Manual work is faster. Edit > Select All > Rename > Enter 1.jpg. Press Enter.
Floor 5 Posted 2007-04-13 23:29 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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)
Floor 6 Posted 2007-04-14 00:03 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
My Windows 2000 can't achieve what Brother htysm said
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 7 Posted 2007-04-14 03:59 ·  中国 湖北 武汉 电信
初级用户
Credits 68
Posts 30
Joined 2007-01-16 07:05
19-year member
UID 76659
Gender Male
Status Offline
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.
Floor 8 Posted 2007-04-14 04:26 ·  中国 浙江 杭州 电信
银牌会员
★★★
Credits 2,000
Posts 621
Joined 2007-01-01 00:00
19-year member
UID 75212
Gender Male
Status Offline
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
Floor 9 Posted 2007-04-15 03:20 ·  中国 湖北 武汉 电信
初级用户
Credits 68
Posts 30
Joined 2007-01-16 07:05
19-year member
UID 76659
Gender Male
Status Offline
### 关于 `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` 值。
Floor 10 Posted 2007-04-15 04:12 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 397
Posts 168
Joined 2006-10-08 10:07
19-year member
UID 64934
Status Offline
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!!
Floor 11 Posted 2007-05-28 13:28 ·  中国 广西 梧州 电信
初级用户
Credits 52
Posts 21
Joined 2007-02-09 11:24
19-year member
UID 79057
Gender Male
Status Offline
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!
Floor 12 Posted 2007-05-28 13:45 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
```
@echo off
for /f "delims=" %%a in ('dir /b *.jpg') do set /a count+=1&&call ren "%%a" "%%count%%.jpg"
```
Floor 13 Posted 2007-05-28 14:11 ·  中国 广东 清远 联通
高级用户
★★
Credits 846
Posts 247
Joined 2006-10-27 12:03
19-year member
UID 68504
Gender Male
From 湖南==》广东
Status Offline
Further "simplify":
@echo off
for %%a in (*.jpg) do set /a count+=1&&call ren "%%a" "%%count%%.jpg"
Floor 14 Posted 2007-05-28 21:19 ·  中国 重庆 奉节县 电信
中级用户
★★
Credits 235
Posts 109
Joined 2006-08-24 00:52
19-year member
UID 61161
Gender Male
Status Offline
Got another harvest. Thanks
Floor 15 Posted 2007-09-03 09:54 ·  中国 广西 南宁 西乡塘区 电信
中级用户
★★
Credits 259
Posts 164
Joined 2006-09-21 23:39
19-year member
UID 63296
Status Offline
Good stuff, included ^_^ Monday, September 3, 2007 9:44:54
Forum Jump: