- 1 year ago
How do I remove all characters after (and including) double underscores in a set of filenames in Windows?
Comment
1 / 1
To remove all characters after (and including) double underscores in a set of filenames in Windows, you can use a batch script. Here's a sample batch script that does the job:
batch@echo off
setlocal enabledelayedexpansion
rem Set the directory path where the files are located
set "directory=C:\path\to\your\folder"
rem Loop through all the files in the directory
for %%F in ("%directory%\*") do (
rem Get the filename without extension
set "filename=%%~nF"
rem Find the position of the last double underscore in the filename
for /F "tokens=1,* delims=__" %%A in ("!filename!") do (
set "filename=%%A"
)
rem Rename the file with the new filename
ren "%%F" "!filename!%%~xF"
)
echo All filenames have been processed.
Make sure to replace C:\path\to\your\folder
with the actual path to the directory containing the files. Save the script with a .bat extension and run it by double-clicking on it.
This script uses a nested loop to find the position of the last double underscore in the filename and then renames the file with the new filename without the characters after the last double underscore.