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