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-12 07:16
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Problems with REN modifying filenames in multiple subdirectories simultaneously View 2,067 Replies 8
Original Poster Posted 2010-05-28 20:14 ·  中国 山东 济南 联通
新手上路
Credits 15
Posts 10
Joined 2010-05-27 22:13
16-year member
UID 167756
Gender Male
Status Offline
Encountered a problem. I have many subdirectories, such as ab001, ab005, ab113... Under the ab001 directory, there is ha0001_00_00.txt, under the ab005 directory, there is ha0012_00_00.txt, under the ab113 directory, there is ha0025_00_00.txt,... I want to implement this function and change the files in each directory to the format of ha????_02_00.txt, that is, change the last few digits of the main file name from _00_00 to _02_00. I used the following command to modify it, but it didn't work. It seems that adding %var:~n,m% doesn't work. for /f "delims=" %a in ('dir /s /b /a-d D:\DOS\ren\ha????_00_00.txt') do echo ren "%a" "%~na.txt" Ask experts, how to modify it? Or is there any other method?
Floor 2 Posted 2010-05-28 22:14 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Batch processing:
@echo off&setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /s /b /a-d D:\DOS\*.txt') do (
set a=%%~na
set "a=!a:_00_00=_02_00!"
ren "%%a" "!a!.txt"
)
Floor 3 Posted 2010-05-28 23:45 ·  中国 山东 济南 联通
新手上路
Credits 15
Posts 10
Joined 2010-05-27 22:13
16-year member
UID 167756
Gender Male
Status Offline
Thanks to user 2nd floor. It is possible to make the dat file according to the code of user 2nd floor. I want to ask if there is any code that can be directly pasted into the command window to run without making a dat file. That is, modifying the code of user 2nd floor into one line so that it can be used directly by copying to the DOS window. Is it by modifying %? I tried but didn't succeed. Can user 2nd floor help to modify it again?

[ Last edited by firstabc on 2010-5-28 at 23:58 ]
Floor 4 Posted 2010-05-29 01:05 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
In batch processing:
setlocal enabledelayedexpansion&for /f "delims=" %%a in ('dir /s /b /a-d D:\rec\*.txt') do (set a=%%~na&set "a=!a:_00_00=_02_00!"&ren "%%a" "!a!.txt")


Run in cmd or directly in Start - Run:
cmd /q /v:on /k "for /f "delims=" %a in ('dir /s /b /a-d D:\rec\*.txt') do (set a=%~na&set "a=!a:_00_00=_02_00!"&ren "%a" "!a!.txt")"


[ Last edited by Hanyeguxing on 2010-5-30 at 00:46 ]
Floor 5 Posted 2010-05-29 01:08 ·  中国 山东 济南 联通
新手上路
Credits 15
Posts 10
Joined 2010-05-27 22:13
16-year member
UID 167756
Gender Male
Status Offline
I copied this code and ran it under DOS (XP system). It came out replacing with!a!.txt. Is it that!a! doesn't work and it can only be used in a bat file?

--------------------Running result 1--------------------------------------------
D:\>cd D:\DOS\ren\abc

D:\DOS\ren\abc>for /f "delims=" %a in ('dir /s /b /a-d D:\DOS\ren\abc\*.txt') do
(set a=%~na&set "a=!a:_00_00=_02_00!"&ren "%a" "!a!.txt")

D:\DOS\ren\abc>(set a=ds & set "a=!a:_00_00=_02_00!" & ren "D:\DOS\ren\abc\ds.
txt" "!a!.txt" )

D:\DOS\ren\abc>(set a=h0012_00_00 & set "a=!a:_00_00=_02_00!" & ren "D:\DOS\re
n\abc\ab\h0012_00_00.txt" "!a!.txt" )

D:\DOS\ren\abc>


--------------------Running result 2--------------------------------------------
D:\>cd D:\DOS\ren\abc\

D:\DOS\ren\abc>setlocal enabledelayedexpansion&for /f "delims=" %a in ('dir /s /
b /a-d D:\DOS\ren\abc\*.txt') do (set a=%~na&set "a=!a:_00_00=_02_00!"&ren "%a"
"!a!.txt")

D:\DOS\ren\abc>(set a=!a! & set "a=!a:_00_00=_02_00!" & ren "D:\DOS\ren\abc\!a
!.txt" "!a!.txt" )

D:\DOS\ren\abc>(set a=!a! & set "a=!a:_00_00=_02_00!" & ren "D:\DOS\ren\abc\ab
\!a!.txt" "!a!.txt" )

D:\DOS\ren\abc>

[ Last edited by firstabc on 2010-5-29 at 01:28 ]
Floor 6 Posted 2010-05-29 16:59 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Re-run
cmd /q /v:on /k "for /f "delims=" %a in ('dir /s /b /a-d D:\rec\*.txt') do (set a=%~na&set "a=!a:_00_00=_02_00!"&ren "%a" "!a!.txt")"


[ Last edited by Hanyeguxing on 2010-5-30 at 00:47 ]
Floor 7 Posted 2010-05-29 20:46 ·  中国 山东 济南 联通
新手上路
Credits 15
Posts 10
Joined 2010-05-27 22:13
16-year member
UID 167756
Gender Male
Status Offline
Can't we just not use the previous cmd /q /v:on /k? Can't the code starting with for be directly copied into the black screen to run?
Floor 8 Posted 2010-05-30 00:54 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
No!NOT!
1. When using setlocal outside of a script or batch file, it will have no effect. So in the cmd window, you cannot use setlocal enabledelayedexpansion to enable variable delay.
2. Therefore, you need to use cmd /q /v:on /k to enable variable delay, where /q turns off echo, /v switches on variable delay, and /k can also be written as /c here.
Floor 9 Posted 2010-06-01 21:55 ·  中国 山东 济南 联通
新手上路
Credits 15
Posts 10
Joined 2010-05-27 22:13
16-year member
UID 167756
Gender Male
Status Offline
Then use Hanyeguxing's batch file, thank you! You can close the thread.
Forum Jump: