The following uses a variable with a
for loop to rename a group of files.
SetLocal EnableDelayedExpansion for %%j in (*.*) do ( set filename=%%~nj set filename=!filename:old=new! set filename=Prefix !filename! set filename=!filename! Suffix ren "%%j" "!filename!%%~xj" )
By defining the variable name
%%j and associating it with all current files
(*.*), we can use the variable in a
for loop to represent each file in the current directory.
Every iteration (or pass) through the loop thereby processes a different file from the defined group (which might equally have been any group, e.g.
In the first example, we substitute text: the text string "old" is replaced by the text string "new" (if, but only if, the text "old" is present in the file's name).
In the second example, we add text: the text "Prefix " is added to the start of the file name. This is not a substitution. This change will be applied to all files in the group.
In the third example, again we add text: the text " Suffix" is added to the end of the file name. Again, this is not a substitution. This change will be applied to all files in the group.
The final line actually handles the renaming.