[英]batch file: how to rename part of multiple directories?
I'm new to batch files and I've been asked to create one which will rename part of multiple directories. 我是批处理文件的新手,因此我被要求创建一个将重命名多个目录的一部分的文件。 For example, I need to rename directories as follows:
例如,我需要重命名目录,如下所示:
ahhh111\\ rename to aggg111\\ ahhh222\\ rename to aggg222\\ ahhh333\\ rename to aggg333\\ and so on ...
I thought I could use move
with wildcards but keep getting syntax errors; 我以为我可以
move
通配符与move
一起使用,但会不断出现语法错误。
eg move "*hhh*" "*ggg*"
throws up a syntax error :-( 例如,
move "*hhh*" "*ggg*"
会引发语法错误:-(
How can I do this please? 我该怎么做?
move
does not accept wildcards when moving/renaming directories. 移动/重命名目录时,
move
不接受通配符。
However, you can use a for /D
loop to iterate through the matching directories and sub-string replacement to build the new name. 但是,您可以使用
for /D
循环遍历匹配的目录并替换子字符串以构建新名称。 For the latter to work in a loop, delayed expansion is required: 为了使后者循环工作,需要延迟扩展 :
for /D %%D in ("?hhh*") do (
set "PDIR=%%~fD" & set "NAME=%%~nxD"
setlocal EnableDelayedExpansion
move "!PDIR!" "!NAME:hhh=ggg!"
endlocal
)
Note that this approach replaces every occurrence of hhh
by ggg
in the directory names. 请注意,此方法用目录名称中的
ggg
替换每次出现的hhh
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.