简体   繁体   中英

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;
eg move "*hhh*" "*ggg*" throws up a syntax error :-(

How can I do this please?

move does not accept wildcards when moving/renaming directories.

However, you can use a for /D loop to iterate through the matching directories and sub-string replacement to build the new name. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM