Advertisement
Uli32

rename_srt.bat

Dec 15th, 2024 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 1.50 KB | None | 0 0
  1. @echo off
  2. setlocal enabledelayedexpansion
  3.  
  4. echo It require a few steps, preparation:
  5. echo 1. extract .SRT from .MKV files, where episode number is on line in format like: [Episode 27]
  6. echo 2. rename .SRT files as .MKV videos
  7. echo 3. run this .BAT file
  8.  
  9. :: Iterate through all .srt files in the current directory
  10. for %%f in (*.srt) do (
  11.     set "episode_number="
  12.  
  13.     :: Extract the line containing '[episode N]' and parse the episode number
  14.     for /f "tokens=2 delims=[] " %%a in ('findstr /i /c:"[episode" "%%f"') do (
  15.         set "episode_number=%%a"
  16.     )
  17.  
  18.     :: Check if an episode number was found
  19.     if defined episode_number (
  20.        :: Construct the new base filename for both SRT and MKV files
  21.         set "base_filename=episode_!episode_number!"
  22.  
  23.         :: Rename the SRT file
  24.         ren "%%f" "!base_filename!.srt"
  25.         echo Renamed SRT file: %%f to !base_filename!.srt
  26.  
  27.         :: Construct the corresponding MKV filename
  28.        :: set "mkvname=!base_filename!.mkv"
  29.         set "mkvname=%%f"
  30.         set "mkvname=!mkvname:.srt=.mkv!"
  31.  
  32.         :: Check if the MKV file exists before renaming
  33.         if exist "!mkvname!" (
  34.            :: Rename the MKV file
  35.             ren "!mkvname!" "!base_filename!.mkv"
  36.             echo Renamed MKV file: !mkvname! to !base_filename!.mkv!
  37.         ) else (
  38.             echo WARNING: Corresponding MKV file !mkvname! not found for %%f
  39.         )
  40.     ) else (
  41.         echo WARNING: No episode number found in file %%f
  42.     )
  43. )
  44.  
  45. endlocal
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement