Batch file Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
stathis
Posts: 7
Joined: 23 Oct 2013 11:55

Batch file Help

#1 Post by stathis » 23 Oct 2013 11:57

Guys i need your help with some batch programming. I need a script to check a string like the one below

greys.anatomy.1005.hdtv-lol.mp4

If it finds 4 consecutive numbers that start with 10-19 it should replace it with S10E
so 1001 becomes S10E01
1524 becomes S15E24
but 2012 remains 2012

Any help is appreciated.

Thanks a lot

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file Help

#2 Post by ShadowThief » 23 Oct 2013 12:34

Code: Select all

:: TV warez renamer
:: Corrects the release name to include season and episode indicators
:: I've literally never seen a release labelled like that but google says its real
@echo off
setlocal enabledelayedexpansion

:: Ex. greys.anatomy.1005.hdtv-lol.mp4 will be renamed to greys.anatomy.s10e05.hdtv-lol.mp4
if "%1"=="" echo Please provide a release name.&exit /b
set release_name=%1

:: Iterate through each element of the release name because they're all different
set counter=0
:parse_name
for /f "tokens=1,* delims=." %%A in ("!release_name!") do (
   set name_fragment[!counter!]=%%A
   set release_name=%%B
   set /a counter+=1
)
if not "!release_name!"=="" (
   goto parse_name
)

:: Puts an S at the start of the fragment and an E before the last two numbers
:: It really helps that no TV show has more than 100 episodes in a season
:: And that a release name ALWAYS starts with the name of the show
set filename=!name_fragment[0]!
for /L %%A in (1,1,!counter!) do (
   if !name_fragment[%%A]! gtr 100 (
      if !name_fragment[%%A]! lss 3000 (
         set season=!name_fragment[%%A]:~0,-2!
         set episode=!name_fragment[%%A]:~-2!
         set name_fragment[%%A]=s!season!e!episode!
      )
   )
   set filename=!filename!.!name_fragment[%%A]!
)

echo !filename!
:: ren !release_name! !filename!

I commented out the part where it actually renames the file because it's not on my system, but the echo is there to show that it works. Just pass in the filename as a parameter.

stathis
Posts: 7
Joined: 23 Oct 2013 11:55

Re: Batch file Help

#3 Post by stathis » 25 Oct 2013 08:18

Thanks for this.

When using "as is" it seems that it works ok.
Edit. It creates a full stop at the end of the !file_name!

Code: Select all

E:\>test.bat ncis.1105.hdtv-lol.mp4
ncis.s11e05.hdtv-lol.mp4.
E:\>


When uncommenting the rename command though i get a "The syntax of the command is incorrect" message.

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file Help

#4 Post by ShadowThief » 25 Oct 2013 15:29

Alright, looks like I had an off-by-one error in that for /L loop. Also, I forgot that I had deconstructed the %release_name% variable to get the different parts of it, so I made a backup of the variable and now I'm using that to rename the file.

Code: Select all

@echo off
setlocal enabledelayedexpansion

:: Ex. greys.anatomy.1005.hdtv-lol.mp4 will be renamed to greys.anatomy.s10e05.hdtv-lol.mp4
if "%1"=="" echo Please provide a release name.&exit /b
set release_name=%1
set old_rn=%release_name%

:: Iterate through each element of the release name because they're all different
set counter=0
:parse_name
for /f "tokens=1,* delims=." %%A in ("!release_name!") do (
   set name_fragment[!counter!]=%%A
   set release_name=%%B
   set /a counter+=1
)
if not "!release_name!"=="" (
   goto parse_name
)

:: Puts an S at the start of the fragment and an E before the last two numbers
:: It really helps that no TV show has more than 100 episodes in a season
:: And that a release name ALWAYS starts with the name of the show
set filename=!name_fragment[0]!
set /a counter-=1
for /L %%A in (1,1,!counter!) do (
   if !name_fragment[%%A]! gtr 100 (
      if !name_fragment[%%A]! lss 3000 (
         set season=!name_fragment[%%A]:~0,-2!
         set episode=!name_fragment[%%A]:~-2!
         set name_fragment[%%A]=s!season!e!episode!
      )
   )
   set filename=!filename!.!name_fragment[%%A]!
)

echo Will rename *%old_rn%* to *!filename!*
ren %old_rn% !filename!

stathis
Posts: 7
Joined: 23 Oct 2013 11:55

Re: Batch file Help

#5 Post by stathis » 25 Oct 2013 23:33

a thousand thanks :) Works perfectly.

It will make my life a lot easier now

stathis
Posts: 7
Joined: 23 Oct 2013 11:55

Re: Batch file Help

#6 Post by stathis » 28 Oct 2013 00:05

ShadowThief i came across a problem with the batch last night.
I realised that sometimes tv show eps have the year in the name as well.

So for example last night i got the following.

Beauty.and.The.Beast.2012.S02E04.HDTV.x264-2HD.mp4

which the script changed to

Beauty.and.The.Beast.s20e12.S02E04.HDTV.x264-2HD.mp4

Is there any way to check for this and avoid it?

Thanks a lot for your help!!!

ShadowThief
Expert
Posts: 1167
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Batch file Help

#7 Post by ShadowThief » 28 Oct 2013 06:20

If you don't have any shows that have gone past 19 seasons, you can just change the 3000 in

Code: Select all

if !name_fragment[%%A]! lss 3000

to 2000.

However, if you've got some long-running shows like The Simpsons or Saturday Night Live, I've taken advantage of the fact that the year is treated like part of the show title and adjusted the script so that it parses the tokens in reverse order and stops once it changes one.

Code: Select all

:showRenamer.bat <releaseName>
:: Some shows are released by groups who don't care about standards, so the
:: release name is sloppy and doesn't include the S or E in the episode ID
:: number. This script corrects that.
::
:: VERSION HISTORY
:: ---------------
:: 1.2 (28 Oct 2013) - Took into account shows that include the starting year in
::                     title for some reason
::                   - Raised the maximum allowable value for an episode ID
::                     after discovering that The Simpsons is on season 25 and
::                     Saturday Night Live is on season 38
::                   - Script aborts if correct episode ID is already present
::                   - Season values less than 10 are now zero-padded
:: 1.1 (25 Oct 2013) - Corrected an off-by-one error in the fragment iterator
::                     that appended a period to the end of the file name
::                   - Used the correct string when renaming the file
:: 1.0 (23 Oct 2013) - Initial Version
@echo off
setlocal enabledelayedexpansion

:: Ex. greys.anatomy.1005.hdtv-lol.mp4 will be renamed to greys.anatomy.s10e05.hdtv-lol.mp4
:: castle.2009.109.hdtv-lol.mp4 should become castle.2009.s01e09.hdtv-lol.mp4
:: Beauty.and.The.Beast.2012.S02E04.HDTV.x264-2HD will not change
if "%1"=="" echo Please provide a release name.&exit /b
set release_name=%1
set old_rn=%release_name%

:: Iterate through each element of the release name because they're all different
set counter=-1
:parse_name
for /f "tokens=1,* delims=." %%A in ("!release_name!") do (
   set /a counter+=1
   set name_fragment[!counter!]=%%A
   set release_name=%%B
)
if not "!release_name!"=="" (
   goto parse_name
)

:: Puts an S at the start of the fragment and an E before the last two numbers.
:: It really helps that no TV show has more than 100 episodes in a season and
:: that a release name ALWAYS starts with the name of the show.
:: Sometimes the year is included, which used to break the script. Fortunately,
:: the year is considered part of the title and therefore comes before the
:: episode ID, so we'll just process the fragments backwards and stop processing
:: once a fragment has been altered.
set filename=!name_fragment[0]!
for /L %%A in (!counter!,-1,1) do (
   if /I "!name_fragment[%%A]:~0,2!"=="S0" goto breakLoop
   if !name_fragment[%%A]! gtr 100 (
      if !name_fragment[%%A]! lss 4000 (
         set season=!name_fragment[%%A]:~0,-2!
         set episode=!name_fragment[%%A]:~-2!
         if !season! lss 10 set name_fragment[%%A]=s0!season!e!episode!
         if !season! geq 10 set name_fragment[%%A]=s!season!e!episode!
         goto breakLoop
      )
   )
)

:breakLoop
for /L %%A in (1,1,!counter!) do set filename=!filename!.!name_fragment[%%A]!

echo !filename!
ren %old_rn% !filename!

stathis
Posts: 7
Joined: 23 Oct 2013 11:55

Re: Batch file Help

#8 Post by stathis » 28 Oct 2013 11:56

Again fantastic job. Working perfectly. You are a life saver. Thanks a lot!!

Post Reply