I've been searching for the past several hours and cannot find a simple method to do this.
SCENARIO:
PARENT_FOLDER
----YEAR_FOLDER
--------MOVIE1_FOLDER
----------Movie1_Title.Ext
----------Eng.Srt
--------MOVIE2_FOLDER
----------Movie2_Title.Ext
----------02_ENG.SRT
----------03_ENG.SRT
--------MOVIE3_FOLDER
----------Movie3_Title.Ext
----------ENG.SRT
----------ENG-SDH.SRT
--------MOVIE4_FOLDER
----------Movie4_title.ext
----------Movie4.Randomstuff.SRT
--------MOVIE5_FOLDER
----------Movie5_title.ext
----------randomstuff.eng.srt
I would like to find some way to create a batch script to recursively go through and rename the SRT files to that of the VIDEO file (AVI, MKV, or MP4). In case where there are 2, I would like to use the largest one if possible or if the SRT name has an SDH to have it added to the end (Movie_Title.SDH.srt).
I've tried:
@echo off
if exist "*SDH.srt" (
for %%f in ("*.avi") do ren *.srt "%%~nf.SDH.srt"
) else (
for %%f in ("*.avi") do ren *.srt "%%~nf.srt"
)
for %%f in ("*.avi") do ren *.srt "%%~nf.srt"
The last line was to get the second file if there was one (I tried it without it as well)... This didn't work at all. I had 2 files, one with the SDH and it didn't rename it correctly.
The only thing that worked was:
for %%f in ("*.avi") do ren *.srt "%%~nf.srt"
It only renamed the first one found and it had to be run within the directory. I added the /R after the "for" but it wouldn't work recursively. I've tried to break it down and do it in steps using sample directories and file names, but that just let to a whole new level of brainhurt.
Am I even close to getting this to work?
@ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir\t w o"
FOR /d /r "%sourcedir%" %%a IN (*) DO (
REM %%a contains directory name. Look for movie file
FOR %%x IN (mkv avi mp4) DO (
IF EXIST "%%a\*.%%x" FOR %%m IN ("%%a\*.%%x") DO (
REM Look for *SDH.SRT file
IF EXIST "%%a\*SDH.SRT" (ECHO REN "%%a\*SDH.SRT" "%%~nmSDH.SRT"
) ELSE (
SET "renamed="
FOR /f "delims=" %%s IN ('dir /b /a-d /o-s "%%a\*.srt"') DO IF NOT DEFINED renamed (
ECHO REN "%%a\%%s" "%%~nm.SRT"
SET "renamed=Y"
)
)
)
)
)
GOTO :EOF
You would need to change the setting of sourcedir
to suit your circumstances.
The required REN commands are merely ECHO
ed for testing purposes. After you've verified that the commands are correct, change ECHO REN
to REN
to actually rename the files.
First run a recursive scan for directory-names into %%a
. With each directory found, look for one of the required extensions in %%x
(I'm presuming there will be only one in any directory) and if one exists, then set %%m
to its name.
Look for *SDH.SRT
in the directory located, and if it exists, rename it with the name-part of %%m
before SDH.SRT
. If there is no *SDH.SRT
file, Clear the renamed
flag then perform a directory-scan of the *.srt
filenames in the directory in reverse-size order so the largest is the first listed. The for/f
assigns the filenames found in turn and in full to %%s
and thus the required largest-.srt file can be renamed, then set the renamed
flag to a value so that it becomes defined and hence further *.srt
files found in the directory will not be renamed.
I deliberately used a directory containing spaces in its name for my testing, so the processing with file/directorynames containing spaces seems correct. I'll make no comment about how missing *.srt
files or filenames containing poison characters may fare.