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-23 01:57
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Solved帖] About the renaming algorithm of ACDSee View 2,370 Replies 21
Original Poster Posted 2008-07-28 20:59 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
For example, if the template is set as Head_???_Body_?_Tail, incrementing from 1 to 1001, the following names should be generated:

Head_001_Body_1_Tail
Head_002_Body_2_Tail
Head_003_Body_3_Tail
...
Head_010_Body_10_Tail
Head_011_Body_11_Tail
...
Head_1000_Body_1000_Tail
Head_1001_Body_1001_Tail

The purpose is not for practical use, mainly to exercise. The template is customized by the user, and there may be up to 255 wildcards "?" in the template, such as???????????Head?????????????Body??Tail?
After thinking for 1 day, there is no breakthrough in efficiency. Please help, thank you!

Thanks to bat-zw and 523066680 for their enthusiastic help here

[ Last edited by obsolete on 2008-7-31 at 10:30 PM ]
Floor 2 Posted 2008-07-28 21:01 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
The program might be rather wordy. If it's troublesome to write, just mention the idea. Thanks!
Floor 3 Posted 2008-07-28 21:25 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
It is roughly in line with the meaning but needs improvement... However, everyone will be relatively... after all, too many people ask about batch renaming. I think it's important for the title to be attractive.

rem This version is edited with if_exist http://hi.baidu.com/523066680
@echo off&setlocal enabledelayedexpansion
set /a i=0,j=0,k=0,l=0
for /f "tokens=*" %%a in ('dir *.jpg /b') do (
set /a l+=1
if !l! equ 10 (set k=)
if !l! equ 100 (set j=)
if !l! equ 1000 (set i=)
ren %%a Head_!i!!j!!k!!l!_Body_!l!_Tail.jpg >nul 2>nul
)


=--------------------------The following code should be able to fully conform-------------------
rem This version is edited with if_exist http://hi.baidu.com/523066680
@echo off&setlocal enabledelayedexpansion
set /a j=0,k=0,l=0
for /f "tokens=*" %%a in ('dir *.jpg /b') do (
set /a l+=1
if !l! equ 10 (set k=)
if !l! equ 100 (set j=)
ren %%a Head_!j!!k!!l!_Body_!l!_Tail.jpg >nul 2>nul
)
=---------------------------------------------------------------------------
But when browsing the page, 001-099 are in the front, 1000-???? are in the middle
100-999 are at the back... There's no way, the computer sorts by the leading characters...
I thought something was missing when I first looked at it.

[ Last edited by 523066680 on 2008-7-28 at 10:15 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
obsolete +2 2008-07-28 21:49
Floor 4 Posted 2008-07-28 21:38 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
Thanks topper.

High efficiency, I didn't think of this method all day TT,
The problem is not universal enough, the template is set by the user himself, maybe the final string splicing will be very difficult
Floor 5 Posted 2008-07-28 21:45 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
If you think it's good, give some extra points! Then I thank you! I'll modify it later to see if it can fully meet the requirements.

The following content does not represent my position.

Welcome to the group 61377162 established by前辈 hat.

[ Last edited by 523066680 on 2008-7-28 at 09:52 PM ]
Floor 6 Posted 2008-07-28 22:49 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
The following code is very inefficient, let's post it first


@echo off
setlocal enabledelayedexpansion

set wildchars=?
for /l %%i in (1,1,8) do (
set wildchars=!wildchars!!wildchars!
)

set zeroes=!wildchars:?=0!
set template=?a??b???c????d?????e??????f???????g????????h?????????i??????????j???????????k????????????l?????????????m??????????????n???????????????o????????????????p?????????????????q??????????????????r???????????????????s????????????????????t?????????????????????u??????????????????????

set tpl=%template%
for /l %%i in (255,-1,1) do (
set wcs=!wildchars:~0,%%i!
call,set temp=%%tpl:!wcs!=%%
if not "!tpl!"=="!temp!" (set occr=!occr! %%i & set tpl=!temp!)
)

for /l %%a in (1,1,1001) do (
set num=%%a
set str=%zeroes%%%a
set file=%template%
for %%b in (!occr!) do (
set wcs=!wildchars:~0,%%b!
set temp=!num:~%%b!
if defined temp (set n=%%a) else set n=!str:~-%%b!
call,set file=%%file:!wcs!=!n!%%
)
echo !file!
echo.
)

pause


[ Last edited by obsolete on 2008-7-28 at 11:20 PM ]
Floor 7 Posted 2008-07-28 23:03 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
The worst - case template above, on the premise that the file name length does not exceed 255, is:
?a??b???c????d?????e??????f???????g????????h?????????i??????????j???????????k????????????l?????????????m??????????????n???????????????o????????????????p?????????????????q??????????????????r???????????????????s????????????????????t?????????????????????u??????????????????????

That is, wildcards from 1 to 22 all appear. It can be approximately calculated like this:
x*(x+1)/2 + x - 1 = 255
Floor 8 Posted 2008-07-29 10:12 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Sweat……I don't know what you're talking about……Mathematics Batch processing……Oh my goodness~
(The most hated mathematics! But there's no way, I also love mathematics, there is love there is hate……)
Floor 9 Posted 2008-07-29 22:03 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
The most time-consuming part in the algorithm of floor 6 is call and set. The purpose of algorithm improvement is to remove this.

Improved algorithm:

@echo off
setlocal enabledelayedexpansion

set template=?a??b???c????d?????e??????f???????g????????h?????????i??????????j???????????k????????????l?????????????m??????????????n???????????????o????????????????p?????????????????q??????????????????r???????????????????s????????????????????t?????????????????????u??????????????????????

set template=!template:?=:?!
set template=!template:?:=/!
set template=!template::=!

set colons=:::::::::

for /l %%a in (1,1,1001) do (
set file=%template%
set n=0
set num=%%a
for /l %%b in (1,1,9) do (
set /a "num=num/10"
if not !num!==0 set /a "n+=1"
)

call,set rep=%%colons:~0,!n!%%%%a
call,set file=%%file:?=!rep!%%
for /l %%i in (1,1,9) do (
set file=!file:/:=!
)
set file=!file:/=0!
set file=!file::=!
echo !file!
echo.
)

pause


[ Last edited by obsolete on 2008-7-30 at 10:05 PM ]
Floor 10 Posted 2008-07-29 22:04 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
The algorithm on floor 9 also uses a premise that the maximum value of an integer is around 2.1 billion, which is 10 digits. Those exceeding 10 digits cannot be processed (because the for loop does not support it)

[ Last edited by obsolete on 2008-7-29 at 10:08 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
523066680 +2 2008-07-30 08:54
Floor 11 Posted 2008-07-30 09:49 ·  中国 广东 广州 电信
银牌会员
★★★★
SuperCleaner
Credits 2,362
Posts 1,133
Joined 2008-02-02 21:36
18-year member
UID 110072
Gender Male
Status Offline
Hey, it's just renaming. Why be so demanding...
I wrote a multi - choice batch for renaming
:: This version is edited for if_exist http://hi.baidu.com/523066680
@echo off
:write - geshi
set /p geshi="Please enter the file format to be renamed, such as jpg:"
if not exist *.%geshi% echo. There is no such format &goto write - geshi
if "%geshi%"=="" echo. There is no such format &goto write - geshi
cls
:write - left
echo. Enter the characteristic part of the file name. Enter a - and it will be named a - 001, a - 002...
echo. Press Enter directly and the name will be the serial number directly
echo. Do not enter special characters
set /p tezheng=":"
:write - lei
set /p lei="Select the type, enter 001 or 1 :"
if "%lei%" == "001" (goto :001)
if "%lei%" == "1" (goto :1) else (echo. Incorrect input &goto :write - lei)


:001
setlocal enabledelayedexpansion
set /a i=0,j=0,k=0
for /f "tokens=*" %%a in ('dir *.%geshi% /b') do (
set /a k+=1
if !k! equ 10 (set j=)
if !k! equ 100 (set i=)
ren %%a %tezheng%!i!!j!!k!.%geshi% >nul 2>nul
echo !i!!j!!k!
)
echo over! Press any key to continue &pause>nul &exit

:1
setlocal enabledelayedexpansion
set /a k=0
for /f "tokens=*" %%a in ('dir *.%geshi% /b') do (
set /a k+=1
ren %%a %tezheng%!k!.%geshi%
echo !k!
)
echo over! Press any key to continue &pause>nul &exit



---------------------------------------------------------------------------------
Regarding the words of the 13th floor, uh... I've already said it. Don't be so demanding...
The above already has many variations

[ Last edited by 523066680 on 2008 - 7 - 30 at 03:06 PM ]
Floor 12 Posted 2008-07-30 12:43 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
Well, I also plan to write a batch script for renaming. The main idea is to make it general - purpose so that every time I need to use it, I can just change a few variables, which is convenient.
Floor 13 Posted 2008-07-30 12:47 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
The method on the 11th floor is much faster than the one on the 9th floor in some common tasks. The only shortcoming is that it is not universal; if the template changes, the program needs to be modified.
Floor 14 Posted 2008-07-30 22:41 ·  中国 浙江 杭州 电信
初级用户
★★
Credits 192
Posts 72
Joined 2005-07-31 21:10
20-year member
UID 41234
Status Offline
Please help everyone, there is only one last call and set left

@echo off
setlocal enabledelayedexpansion

set template=?a??b???c????d?????e??????f???????g????????h?????????i??????????j???????????k????????????l?????????????m??????????????n???????????????o????????????????p?????????????????q??????????????????r???????????????????s????????????????????t?????????????????????u??????????????????????

set template=!template:?=:?!
set template=!template:?:=/!
set template=!template::=!

for /l %%a in (1,1,1001) do (
set file=%template%
set temp=%%a:::::::::
set rep=!temp:~10!%%a
call,set file=%%file:?=!rep!%%
for /l %%i in (1,1,9) do (
set file=!file:/:=!
)
set file=!file:/=0!
set file=!file::=!
echo !file!
echo.
)

pause


[ Last edited by obsolete on 2008-7-30 at 10:50 PM ]
Floor 15 Posted 2008-07-30 22:49 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
A very simple rename program is made to be like天书, the original poster should not lose the larger point for the small matter!!!
批处理之家新域名:www.bathome.net
Forum Jump: