- 1 year ago
Batch renaming files to remove prefix and characters in the middle of file name
To batch rename files in Windows using a batch script to remove a prefix and characters in the middle of the file name, you can use the ren
command (short for rename
). Here's an example:
batch@echo off
setlocal enabledelayedexpansion
set "sourceFolder=C:\Path\to\folder"
set "prefixToRemove=prefix_"
set "charactersToRemove=_"
for %%F in ("%sourceFolder%\%prefixToRemove%*%charactersToRemove%*") do (
set "filename=%%~nxF"
set "newFilename=!filename:%prefixToRemove%=!"
set "newFilename=!newFilename:%charactersToRemove%=!"
ren "%%F" "!newFilename!"
)
echo File names renamed successfully.
Make sure to adjust the sourceFolder
variable to the path of the folder containing the files you want to rename. Set the prefixToRemove
variable to the prefix you want to remove from the file names, and set the charactersToRemove
variable to the characters you want to remove from the middle of the file names.
In the script, it iterates through the files in the source folder using a for
loop with a wildcard pattern that matches the files with the specified prefix and characters in the middle. For each matching file, it extracts the file name and removes the prefix and characters using string substitution. Finally, it uses the ren
command to rename the file with the new file name.
Please note that this script assumes that the source folder only contains the files you want to rename. If there are other files or subfolders in the source folder, you may need to modify the script to handle those cases accordingly.
Save the script with a .bat
extension and run it from the command prompt. It will rename the files according to the specified rules.
Remember to test the script on a sample set of files before running it on your actual files to ensure it behaves as expected and meets your requirements.