![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-12 06:16 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » Problems with REN modifying filenames in multiple subdirectories simultaneously |
| Printable Version 2,065 / 8 |
| Floor1 firstabc | Posted 2010-05-28 20:14 |
| 新手上路 Posts 10 Credits 15 | |
|
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?
|
|
| Floor2 Hanyeguxing | Posted 2010-05-28 22:14 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
Batch processing:
|
|
| Floor3 firstabc | Posted 2010-05-28 23:45 |
| 新手上路 Posts 10 Credits 15 | |
|
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 ] |
|
| Floor4 Hanyeguxing | Posted 2010-05-29 01:05 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
In batch processing:
Run in cmd or directly in Start - Run: [ Last edited by Hanyeguxing on 2010-5-30 at 00:46 ] |
|
| Floor5 firstabc | Posted 2010-05-29 01:08 |
| 新手上路 Posts 10 Credits 15 | |
|
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 ] |
|
| Floor6 Hanyeguxing | Posted 2010-05-29 16:59 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
Re-run
[ Last edited by Hanyeguxing on 2010-5-30 at 00:47 ] |
|
| Floor7 firstabc | Posted 2010-05-29 20:46 |
| 新手上路 Posts 10 Credits 15 | |
|
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?
|
|
| Floor8 Hanyeguxing | Posted 2010-05-30 00:54 |
| 银牌会员 Posts 897 Credits 1,039 From 在地狱中仰望天堂 | |
|
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. |
|
| Floor9 firstabc | Posted 2010-06-01 21:55 |
| 新手上路 Posts 10 Credits 15 | |
|
Then use Hanyeguxing's batch file, thank you! You can close the thread.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |