.bat file for renaming multiple folders

后端 未结 5 1608
别那么骄傲
别那么骄傲 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 07:36

    You can use the following command within your batch file:-

    for /F "usebackq tokens=*" %%a in (`dir /ad /b %1`) do ren %1\%%a %%a%2
    

    This is the DOS 'for' command, which iterates over given set of items, and for each element in the set, performs the given action. For the given requirement, we need to do the following:-

    1) Accept name of folder which contains sub-folders to be renamed(in your example, it is Workspace).

    2) Accept the string to be appended to the end(in your example, it is your name).

    3) List the names of sub-folders in the folder.

    4) Rename by appending the string to original name.

    Let's see how this for command accomplishes that. The format of 'for' command used here is:-

    for /F ["options"] %variable IN (`command`) do command [command-parameters]
    

    The command here assumes that the required parent directory name and string to be appended are passed on as command line parameters. These are represented by %1 and %2 (first and second parameters).

    To enable us to issue a dos command to be evaluated, we need to use the /F option. The option string is :-

    "usebackq tokens=*"
    
    • usebackq specifies backquouted string is a command to be evaluated.(Note that the dir command is enclosed within backquotes(`) )
    • tokens=* means to consider each line as a single token and pass to the command

    To list the sub-directories in parent directory, we use the command:-

    dir /ad /b %1
    
    • /ad displays only directories (ignores files)
    • /b displays it in bare format, i.e., only names are returned and date, time and other info are not.
    • %1 is the command line variable referring to parent directory.
    • %%a is the variable which receives the sub-directory name in each iteration. Double percentage symbol is required since we use it in a batch file, otherwise, just one is required (like %a)

    Finally, we specify the action to be performed:-

    ren %1\%%a %%a%2
    
    • %1\%%a constructs absolute path to sub-directory
    • %%a%2 append second command line parameter to original name

    For more info on for command, type following in a command prompt:-

    for /?
    

    For another usage example, refer Loopy loops: The DOS way

    0 讨论(0)
  • 2020-12-10 07:43

    If there is no need to perform folder/file renaming with a batch file, you can also use the Bulk Rename Utility for Windows. Check this: http://www.bulkrenameutility.co.uk/Download.php

    0 讨论(0)
  • 2020-12-10 07:44

    No need for a batch file. This will work from the command line

    for /d %D in ("Workspace\*") do ren "%D" "%~nxD_myName"
    

    If you do use a batch file, then %D must become %%D

    0 讨论(0)
  • 2020-12-10 07:55
    For /D %%f in (*) do rename "%%f"  "%%fWhatEverNameYouLike"
    pause
    

    The pause is to see it! Make a cmd of it and put it in the folder that you want to rename all it's subfolders! They'll rename to each one folders name, plus the WhatEverNameYouLike

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题