.bat to rename multiple folders

I am trying to write a script package to rename multiple folders. I would like to do something like the following: Rename all the folders in the Workspace folder by adding my name at the end of the folder names

For example, rename:

Workspace/RiskFolder Workspace/PNLFolder 

in

 Workspace/RiskFolder_myname Workspace/PNLFolder_myname 

Is it possible?

+6
source share
5 answers

You can use to scroll through each directory and rename it like this:

 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 on Windows XP.

What this does: for each directory in the path (in brackets), assign the directory name to the variable %%f , and then rename the %%f directory to the name in the format you need (with your name attached). %%f contains the full path name, which is great for the first argument of the rename command, but for the second argument we only need the + filename extension, so the ~ nx modifier is added to our variable name.

By the way, when using this for loop on the command line (and not in the batch file part) you want to use only one% instead of %% for your variable name. For instance. for %f in... instead of above.

For more information, see the following links from Microsoft:

+14
source

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 use a batch file, then %D should become %%D

+3
source

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

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

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

1) Accept the name of the folder that contains the subfolders for renaming (in your example, this is Workspace).

2) Accept the line that will be added to the end (in your example, this is your name).

3) List the names of the subfolders in the folder.

4) Rename by adding a line to the original name.

Let's see how this for the team accomplishes this. The "for" command format used here is: -

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

This command assumes that the required parent directory name and the added line are passed as command line parameters. They are represented by% 1 and% 2 (first and second parameters).

To allow us to issue a dos command for evaluation, we need to use the / F option. Options String: -

 "usebackq tokens=*" 
  • usebackq indicates that the backquouted string is the command to be evaluated. (Note that the dir command is enclosed in backquotes (`))
  • tokens = * means treat each line as one token and send a command

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

 dir /ad /b %1 
  • / ad only displays directories (ignores files)
  • / b displays it as a bare format, i.e. only names are returned, and date, time and other information are missing.
  • % 1 is a command line variable related to the parent directory.
  • %% a is a variable that gets the name of the subdirectory at each iteration. A double percentage symbol is required, since we use it in a batch file, otherwise only one is needed (for example,% a)

Finally, we indicate the action to be performed: -

 ren %1\%%a %%a%2 
  • % 1 \ %% a builds an absolute path to a subdirectory
  • %% a% 2 add a second command line parameter to the original name

For more information about the command, enter the following command at the command prompt: -

 for /? 

For another usage example, see Loopy loops: DOS path

+3
source

If you do not need to rename a folder / file using a batch file, you can also use the Bulk Rename utility for Windows. Check it out: http://www.bulkrenameutility.co.uk/Download.php

+2
source
 For /D %%f in (*) do rename "%%f" "%%fWhatEverNameYouLike" pause 

Pause - see it! Create cmd and put it in the folder you want to rename to all subfolders! They will be renamed to each name of one folder, as well as WhatEverNameYouLike

0
source

Source: https://habr.com/ru/post/908391/


All Articles