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

Categories

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

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

1 Answer

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:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...