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,
[ Last edited by 523066680 on 2009-1-14 at 09:39 ]
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
| Rater | Score | Time |
|---|---|---|
| HAT | +8 | 2009-01-10 16:04 |


