.bat file for renaming multiple folders

后端 未结 5 1618
别那么骄傲
别那么骄傲 2020-12-10 07:12

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 n

5条回答
  •  遥遥无期
    2020-12-10 08:01

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

    for /D %%f in (C:\path\to\Workspace\*) 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:

    • http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
    • http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/for.mspx?mfr=true

提交回复
热议问题