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-06-25 08:24
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Discussion] A vulnerability present in many batch renaming tools View 2,082 Replies 9
Original Poster Posted 2009-01-10 11:20 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Accidentally discovered a bug that many BatchRen have ~ The dir will sort and list the names, but...

For example, there are 0.jpg and 1.jpg. When renaming 0.jpg to 1.jpg for the first time... it will go wrong.

First, talk about the classic
@echo off
for %%a in (*.jpg) do (set /a n+=1 &call ren %%a %%n%%.jpg)

The code is short, but when renaming, many numbers are added some
I renamed files from 001-099, and as a result, numbers above 100 appeared, and I couldn't figure it out
So I echoed it, and it was depressing. That for sentence can catch the renamed files again into the collection... so advanced a function

Advanced list as follows
001.jpg - 1
:
:
085.jpg - 85
086.jpg - 86
087.jpg - 87
088.jpg - 88
089.jpg - 89
090.jpg - 90
091.jpg - 91
092.jpg - 92
093.jpg - 93
094.jpg - 94
095.jpg - 95
096.jpg - 96
097.jpg - 97
098.jpg - 98
099.jpg - 99
1.jpg - 100
10.jpg - 101
11.jpg - 102
12.jpg - 103
13.jpg - 104
14.jpg - 105
15.jpg - 106
16.jpg - 107
:
:

And generally, after renaming once, renaming again will have problems, the numbers are chaotic, repeated contradictions...

Changing to for /f "tokens=*" %%a in ('dir *.jpg /b ') do ( will not have the problem of repeatedly renaming the same file

-----------------------------------------------------------------------

Continue with the previous problem:
dir will sort and list the names, but...
For example, there are 0.jpg and 1.jpg. When renaming 0.jpg to 1.jpg for the first time... it will go wrong.

Temporary solution is -
ren *.jpg *.jpg#
Then rename one by one, and there will be no name conflict. When there are many jpg files, there is a slight pause at the beginning.

Here's my own code, rough,


@echo off
title code by 523066680 @ cn-dos.net
if not exist *.jpg (echo,No jpg files exist&pause>nul&exit)

setlocal enabledelayedexpansion
::Judge the number of digits of the file, decide the maximum number of zeros to pad in front of the name. The number of files cannot be more than 9 digits.
for %%a in (*.jpg) do set /a a+=1
set "a=%a%987654321" & set "o=0000000000"
set /a k=0
::Avoid renaming name conflicts
ren *jpg *.jpg#
for /f "tokens=*" %%a in ('dir *.jpg# /b') do (
set /a k+=1
set name=%o%!k!
ren "%%a" "0!name:~-%a:~9,1%!.jpg"
echo,0!name:~-%a:~9,1%!
)


[ Last edited by 523066680 on 2009-1-14 at 09:39 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
HAT +8 2009-01-10 16:04
Floor 2 Posted 2009-01-10 11:36 ·  中国 北京 联通
银牌会员
★★★★
[b]看你妹啊[/b]
Credits 1,488
Posts 1,357
Joined 2006-05-20 12:00
20-year member
UID 55770
Status Offline
After reading it back and forth twice, I still couldn't figure out what it was talking about.

有问题请发论坛或者自行搜索,再短消息问我的统统是SB
Floor 3 Posted 2009-01-10 11:53 ·  中国 广西 百色 中移铁通
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
I also found this problem before. The first file that was renamed will be renamed again.
Floor 4 Posted 2009-01-10 16:04 ·  中国 重庆 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
It is suggested to add search - friendly keywords in the post title
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
523066680 +5 2009-01-14 09:40
Floor 5 Posted 2009-01-10 16:23 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
The process of for enumeration and ren listing is both gradual, so there will be such characteristics
49206C6F766520796F752067757973 54656C3A3133383238343036373837
Floor 6 Posted 2009-01-14 09:40 ·  中国 广东 揭阳 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Floor 7 Posted 2009-01-14 12:12 ·  中国 江苏 南京 电信
初级用户
★★
Credits 62
Posts 62
Joined 2007-07-14 14:39
18-year member
UID 93962
Gender Male
Status Offline
The building is really careful! Thanks for the reminder! The code is very practical
Floor 8 Posted 2009-01-15 00:53 ·  中国 广东 东莞 电信
银牌会员
★★★
批处理编程迷
Credits 1,916
Posts 752
Joined 2008-12-28 04:30
17-year member
UID 135147
Gender Male
From 广西
Status Offline
::Fast, can modify long filenames
@echo off&setlocal enabledelayedexpansion

for /f "tokens=1 delims= " %%a in ('dir *.jpg ^| findstr 个文件') do set n=%%a
set /a n=n/10+1
set b=0

for /f "tokens=* delims=" %%a in ('dir /b *.jpg ^| findstr .jpg') do (
set name=000000000!b!
set name=!name:~-%n%!
ren "%%a" "!name!.jpg"
set /a b+=1
)
pause

[ Last edited by netbenton on 2009-1-15 at 00:57 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
523066680 +5 2009-01-15 14:15
精简
=> 个人网志
Floor 9 Posted 2009-02-07 17:58 ·  中国 黑龙江 哈尔滨 电信
新手上路
Credits 15
Posts 15
Joined 2009-01-24 10:46
17-year member
UID 137352
Gender Male
Status Offline
I copied a copy of the original poster's source code. Just tried it, only one didn't change the name, which is the picture originally named 14. The others were all changed, even those with mixed numbers and English were fine. But, I can't figure out why the one originally named 14 didn't change. So, I still want to consult the original poster.
Floor 10 Posted 2009-02-07 19:32 ·  中国 福建 三明 电信
中级用户
★★
Credits 458
Posts 211
Joined 2006-07-26 19:42
19-year member
UID 59307
Status Offline
The first "naked" FOR uses CALL to call the label process, and this problem won't occur.
┌───────┐
├→学习→实践→┤
└───────┘
Forum Jump: