Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
629 views
in Technique[技术] by (71.8m points)

windows - .bat file for renaming multiple folders

I am trying to write a batch script to rename multiple folders. I would like to do something like below: Rename all folders under the "Workspace" folder by appending my name in the end of the folder names

For example, rename:

Workspace/RiskFolder
Workspace/PNLFolder

to:

Workspace/RiskFolder_myname
Workspace/PNLFolder_myname

Is this possible?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use for to loop through each directory and rename it like so:

for /D %%f in (C:pathoWorkspace*) do rename "%%f" "%%~nxf_myname"

I tested this on Windows 7, but it should work at least as far back as with Windows XP.

What that does is this: for each directory in the path (within parenthesis), assign the directory name to the variable %%f, then rename the directory %%f to the name in the format you want (with your name attached). %%f holds the full pathname, which is fine for the first argument to the rename command, but for the second argument, we only want the filename+extension, thus the ~nx modifier prepended to our variable name.

By the way, when using this for loop on the command line (rather than part of a batch file) you only want to use one % instead of %% for your variable name. E.g. for %f in... instead of above.

See the following references from Microsoft for more details:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...