|
firstabc
新手上路

积分 15
发帖 10
注册 2010-5-27
状态 离线
|
『楼 主』:
REN同时修改多个子目录下文件名的问题
使用 LLM 解释/回答一下
遇到一个问题,我有很多子目录,比如ab001,ab005,ab113...目录,
ab001目录下有ha0001_00_00.txt,
ab005目录下有ha0012_00_00.txt,
ab113目录下有ha0025_00_00.txt,
......
我想实现这个功能,同时把每个目录下的文件变成
ha????_02_00.txt这种格式的,就是说把主文件名最后几位的
_00_00变成_02_00。
我用下面这个命令来修改,没成功,%var:~n,m%加进去好像也不行。
for /f "delims=" %a in ('dir /s /b /a-d D:\DOS\ren\ha????_00_00.txt') do echo ren "%a" "%~na.txt"
求教高手,怎么改啊?或者还有别的什么方法?
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?
|
|
2010-5-28 20:14 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
批处理: @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"
)
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"
)
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-5-28 22:14 |
|
|
firstabc
新手上路

积分 15
发帖 10
注册 2010-5-27
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢2楼的,根据2楼的代码做出dat文件是可以的,我想要不用做成dat文件的,直接粘贴到命令窗口就可以跑出来的代码有没有啊?就是说把2楼的代码改在一排,直接拷贝到dos窗口就可以用的。是不是改改%啊,我试了下,没成功,能不能请2楼再帮个忙改改啊?
Last edited by firstabc on 2010-5-28 at 23:58 ]
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 ]
|
|
2010-5-28 23:45 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
批处理里: 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")
cmd或在开始-运行中直接运行: 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 ]
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 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-5-29 01:05 |
|
|
firstabc
新手上路

积分 15
发帖 10
注册 2010-5-27
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
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")
我拷贝这个代码在dos下运行了(xp系统),出来替换成了!a!.txt,是不是!a!不行啊,只能在bat文件里用?
--------------------运行结果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>
--------------------运行结果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 ]
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 ]
|
|
2010-5-29 01:08 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
重新运行 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 ]
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 ]
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-5-29 16:59 |
|
|
firstabc
新手上路

积分 15
发帖 10
注册 2010-5-27
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
不要前面的cmd /q /v:on /k 不行吗,直接把以for开头的代码拷贝到黑屏里运行的代码不能实现吗?
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?
|
|
2010-5-29 20:46 |
|
|
Hanyeguxing
银牌会员
     正在学习中的菜鸟...
积分 1039
发帖 897
注册 2009-3-1 来自 在地狱中仰望天堂
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
不可以!
1,在脚本或批处理文件外使用 setlocal 时,将没有效果。所以在cmd窗口内是不能使用setlocal enabledelayedexpansion来开启变量延迟。
2,因此,就要使用cmd /q /v:on /k 来开启变量延迟,其中/q关闭回显,/v开关变量延迟,/k在这里也可以写成/c。
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.
|

批处理之家 http://bbs.bathome.net/forum-5-1.html |
|
2010-5-30 00:54 |
|
|
firstabc
新手上路

积分 15
发帖 10
注册 2010-5-27
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
那就用Hanyeguxing的批文件吧,谢谢你了!可以揭贴了。
Then use Hanyeguxing's batch file, thank you! You can close the thread.
|
|
2010-6-1 21:55 |
|