批处理文件:如何重命名多个目录的一部分?

时间:2017-08-01 09:32:44

标签: windows batch-file

我是批处理文件的新手,我被要求创建一个将重命名多个目录的部分。例如,我需要重命名目录,如下所示:

ahhh111\ rename to aggg111\
ahhh222\ rename to aggg222\
ahhh333\ rename to aggg333\  and so on ...

我以为我可以将move与通配符一起使用,但不断出现语法错误; 例如move "*hhh*" "*ggg*"引发语法错误: - (

我该怎么办?

1 个答案:

答案 0 :(得分:0)

移动/重命名目录时,

move不接受通配符。

但是,您可以使用for /D loop遍历匹配的目录和sub-string replacement来构建新名称。要让后者在循环中工作,需要delayed expansion

for /D %%D in ("?hhh*") do (
    set "PDIR=%%~fD" & set "NAME=%%~nxD"
    setlocal EnableDelayedExpansion
    move "!PDIR!" "!NAME:hhh=ggg!"
    endlocal
)

请注意,此方法会在目录名称中将hhh的{​​{1}}替换为ggg

相关问题