- 1 year ago
Renaming multiple files using Batch script
Renaming multiple files using a batch script is a simple process that can be accomplished using the ren
command in Windows. Here is an example script that renames all .txt
files in the current directory by appending the string "_old" to their filenames:
bash@echo off
setlocal EnableDelayedExpansion
for %%f in (*.txt) do (
set "filename=%%~nf"
set "extension=%%~xf"
ren "%%f" "!filename!_old!extension!"
)
echo All files renamed successfully.
In this script, we use the for
loop to iterate over all .txt
files in the current directory. For each file, we extract the filename and extension using the %%~nf
and %%~xf
variables, respectively. We then use the ren
command to rename the file by appending "_old" to the filename.
The setlocal EnableDelayedExpansion
command is used to enable delayed environment variable expansion, which allows us to use the !
character to expand the value of the filename
variable inside the loop.
Note that you can modify this script to rename files with different extensions or to append a different string to the filenames by changing the file extension in the for
loop and the string in the ren
command, respectively.