不小心发现,好多 BatchRen都有的一个bug ~ dir会把名字排序并列出,但是...
比如存在 0.jpg 1.jpg 第一次ren 0.jpg 为1.jpg 时...就会出错.
首先,谈谈经典的
@echo off
for %%a in (*.jpg) do (set /a n+=1 &call ren %%a %%n%%.jpg)
代码是短,就是ren的时候好多编号都多了一些
我ren 001-099 这些文件,结果出现了100以上的编号,百思不得其解
于是echo了一下,郁闷,那句for居然可以把Ren过的文件再次抓进集合...好先进的功能
先进列表如下
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
:
:
而且一般ren过一次以后再ren 还会出问题的,编号混乱了,重复矛盾了......
改成 for /f "tokens=*" %%a in ('dir *.jpg /b ') do ( 就不会出现重复重命名同一个文件的问题了
-----------------------------------------------------------------------
继续前面说的问题:
dir会把名字排序并列出,但是...
比如存在 0.jpg 1.jpg 第一次ren 0.jpg 为1.jpg 时...就会出错.
临时的解决方案是-
ren *.jpg *.jpg#
接下来挨个重命名就不会出现同名矛盾了。jpg文件多的时候感觉开头停顿了一下下.
来个自己的代码,粗糙的,
@echo off
title code by 523066680 @ cn-dos.net
if not exist *.jpg (echo,不存在jpg文件&pause>nul&exit)
setlocal enabledelayedexpansion
::判断文件数位数,决定名字前面补0的最大个数.文件数不能多于9位数.
for %%a in (*.jpg) do set /a a+=1
set "a=%a%987654321" & set "o=0000000000"
set /a k=0
::避免重命名同名冲突
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 ]
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 ]