Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

SceneRenamer - Renames "Scene" releases of TV Show


  • Please log in to reply
56 replies to this topic
Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
As before, I've edited the current version (with the mods of Paradox):

- Added an option to read and keep the information of the releases.

[quote]e.g:
How.I.Met.Your.Mother.S05E10.HDTV.XviD-FQM

becomes:

How I Met Your Mother - S05E10 - The Window - HDTV.XviD-FQM
or
How.I.Met.Your.Mother.S05E10.The.Window.HDTV.XviD-FQM

instead of

How.I.Met.Your.Mother.S05E10.The.Window
or
How I Met Your Mother - S05E10 - The Window[/quote]

This is very useful to non-English people (like me), to find / produce subtitles!!

here is my version:
- made no changes to xpath.ahk

- SceneRenamer
#Include xpath.ahk

#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%

WindowTitle = SceneRenamer v1.42.5 Modified By Paradox

Gui, Add, Button, Default gButtonAddFolder, &Add folder
Gui, Add, Button, x+20 gButtonAddFile, &Add file
Gui, Add, Button, x+20 gButtonClear, &Clear list
Gui, Add, Text, x+50 w50, Status:
Gui, Add, Edit, +ReadOnly x+0 w500, Waiting for user to add file(s)...

Gui, Add, ListView, xm r26 w800 -ReadOnly vMyListView, Name|Preview|Directory|Extension|TVDB Series ID No

Gui, Add, DropDownList, w300 AltSubmit vNameScheme, (Show Title) - (SS)x(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title)|(SS)x(EE) - Episode Title|S(SS)E(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title) - (Release)||
Gui, Add, DropDownList, x+10 w180 AltSubmit vSeparator, Use spaces as separators||Use.periods.as.separators|Use_underscores_as_separators
Gui, Add, Button, x+150 gButtonGetTVDBInfo, &Get TVDB Info
Gui, Add, Button, x+10 gButtonRename, &Rename files

Gui, Add, Checkbox, xm Checked vZero, Pad with zero?
Gui, Add, Checkbox, x+10 Checked vCache, Cache Episode Info?
Gui, Add, Checkbox, x+10 vRename, Allow Direct Rename?
Gui, Add, Checkbox, x+10 Checked vClear, Clear List after Rename?

Menu, MyContextMenu, Add, Open, ContextOpenFile
Menu, MyContextMenu, Add, Properties, ContextProperties
Menu, MyContextMenu, Add, Remove item, ContextRemoveItem

Gui, Show,, %WindowTitle%
Return

GuiClose:
   ExitApp

GuiContextMenu:
   If A_GuiControl <> MyListView
       Return
   Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
   Return

ContextOpenFile:
ContextProperties:
   FocusedRowNumber := LV_GetNext(0, "F")
   If not FocusedRowNumber
      return
   LV_GetText(FileName, FocusedRowNumber, 1)
   LV_GetText(FileDir, FocusedRowNumber, 3)
   IfInString A_ThisMenuItem, Open
      Run %FileDir%\%FileName%,, UseErrorLevel
   Else
      Run Properties "%FileDir%\%FileName%",, UseErrorLevel
   If ErrorLevel
      MsgBox Could not perform requested action on "%FileDir%\%FileName%".
   return

ContextRemoveItem:
   RowNumber = 0
   Loop
   {
      RowNumber := LV_GetNext(RowNumber - 1)
      If not RowNumber
           Break
      LV_Delete(RowNumber)
   }
   Return

ButtonAddFolder:
   Gui +OwnDialogs
   FileSelectFolder, TargetFolder,, 3, Select a folder:
   If not TargetFolder  ; The user pressed cancel
       Return
   StringRight, LastChar, TargetFolder, 1
   If LastChar = \
      StringTrimRight, TargetFolder, TargetFolder, 1
   GuiControl, -Redraw, MyListView ; Improves performance by disabling redrawing
   Loop %TargetFolder%\*.*
   {
      FileName := A_LoopFileFullPath
      SplitPath, FileName,,, FileExt  ; Get the file extension
      If FileExt in avi,mkv,mp4,srt
         LV_Add(Row, A_LoopFileName, "", A_LoopFileDir, FileExt)
   }
   GuiControl, +Redraw, MyListView  ; Turn redrawing back on
   LV_ModifyCol()  ; Resize each column to fit
   LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
   LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
   LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   ControlSetText, Edit1, Added folder %TargetFolder%, %WindowTitle%
   Return

ButtonAddFile:
   Gui +OwnDialogs
   FileSelectFile, TargetFile
   If not TargetFile
      Return
   SplitPath, TargetFile,,, FileExt  ; Get the file extension
   If FileExt in avi,mkv,mp4,srt
   {
      SplitPath, TargetFile, FileName, TargetFolder
      LV_Add(Row, FileName, "", TargetFolder, FileExt)
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      ControlSetText, Edit1, Added file %TargetFile%..., %WindowTitle%
   }
   Return

ButtonClear:
   ClearList()
   Return

ClearList()
{
   TargetFolder =
   TargetFile =
   LV_Delete()
   ControlSetText, Edit1, Cleared Listview, %WindowTitle%
   Return
}

InternetCheckConnection(Url="",FIFC=1) {
   Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0)
}

ButtonGetTVDBInfo:
   GetTVDBInfo()
   Return

GetTVDBInfo()
{
   LV_GetText(LVFileName, 1, 1)
   If not LVFileName
   {
      Gui +OwnDialogs
      MsgBox, 0,Fatal Error!, You must select a file or folder before connecting to TheTVDB.com!
      Return
   }
   LV_GetText(LVFileDir, 1, 3)  ; Get the file directory
   LV_GetText(LVFileExt, 1, 4)  ; Get the file extension

   URL := "http://www.thetvdb.com"

   If not InternetCheckConnection(URL)
   {
      Msgbox, 48, [%URL%]  , Connection Failed!
      Return
   }

   RowNumber = 1
   ShowInfo := GetTVDBShows(GetShowName(LVFileName, LVFileDir))
   StringSplit, ShowArray, ShowInfo, `,
   ShowName = %ShowArray1%
   SeriesIDNo = %ShowArray2%
   Loop
   {
      Gui +OwnDialogs
      If A_Index > 1
      {
         LV_GetText(LVPrevFN, RowNumber - 1, 1)  ; Get the preceeding rows filename
         LV_GetText(LVPrevFD, RowNumber - 1, 3)  ; Get the preceeding rows file directory
         PrevShowName := GetShowName(LVPrevFN, LVPrevFD)
         StringUpper, UpperPrevSN, PrevShowName

         NewShowName := GetShowName(LVFileName, LVFileDir)
         StringUpper, UpperCurrSN, NewShowName
         If UpperCurrSN <> %UpperPrevSN%
         {
            ShowInfo := GetTVDBShows(NewShowName)
            StringSplit, ShowArray, ShowInfo, `,
            ShowName = %ShowArray1%
            SeriesIDNo = %ShowArray2%
         }
      }
      UpperPrevSN =
      SeasonNumber := GetSeasonNumber(LVFileName)
      EpisodeNumber := GetEpisodeNumber(LVFileName)
      OriginalTitle := GetTitle(LVFileName)
      ReleaseInfo := GetRelease(LVFileName)
      ControlSetText, Edit1, Processing "%ShowName%" Season "%SeasonNumber%" Episode "%EpisodeNumber%", %WindowTitle%
      If SeriesIDNo
      {
         IfInString, ShowName, %A_Space%
            StringReplace, SafeShowName, ShowName, %A_Space%, _, All
         Else
            SafeShowName = %ShowName%
         EpisodeInfo := GetTVDBEpisodes(SeriesIDNo, SeasonNumber, EpisodeNumber, SafeShowName)
         StringSplit, OutputArray, EpisodeInfo, `,
         SeasonNumber = %OutputArray1%
         EpisodeNumber = %OutputArray2%
         EpisodeTitle = %OutputArray3%
         If EpisodeTitle
         {
            StringReplace, EpisodeTitle, EpisodeTitle, \, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, /, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, :, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, *, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, ?,, All
            StringReplace, EpisodeTitle, EpisodeTitle, ", ', All
            StringReplace, EpisodeTitle, EpisodeTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, |, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, &`#44;, `,, All
            StringReplace, EpisodeTitle, EpisodeTitle, &, &, All
         }
         If OriginalTitle
         {
            StringReplace, OriginalTitle, OriginalTitle, \, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, /, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, :, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, *, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, ?,, All
            StringReplace, OriginalTitle, OriginalTitle, ", ', All
            StringReplace, OriginalTitle, OriginalTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, |, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, &`#44;, `,, All
            StringReplace, OriginalTitle, OriginalTitle, &, &, All
         }
      }
      GuiControlGet, PadZero,, Zero
      If PadZero = 1
      {
         SeasonNumber := PadZero(SeasonNumber)
         EpisodeNumber := PadZero(EpisodeNumber)
      }
      GuiControlGet, NamingScheme,, NameScheme
      If SeasonNumber && EpisodeNumber
      {
         If NamingScheme = 1
         {
            If EpisodeTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber%
         }
         Else if NamingScheme = 2
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 3
         {
            If EpisodeTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %SeasonNumber%x%EpisodeNumber%
         }
         Else If NamingScheme = 4
         {
            If EpisodeTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 5
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle% - %ReleaseInfo%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle% - %ReleaseInfo%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %ReleaseInfo%
         }
      }
      Else
      {
         StringReplace, LVFN, LVFileName, \, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, /, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, :, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, *, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, ?,, All
         StringReplace, LVFN, LVFN, ", ', All
         StringReplace, LVFN, LVFN, <, %A_Space%less than%A_Space%, All
         StringReplace, LVFN, LVFN, >, %A_Space%greater than%A_Space%, All
         StringReplace, LVFN, LVFN, |, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, &`#44;, `,, All
         StringReplace, LVFN, LVFN, &, &, All
         StringTrimRight, LVFN, LVFN, 4
         Loop
         {
            StringRight, LastChar, LVFN, 1
            If (LastChar = "-" or LastChar = " ")
               StringTrimRight, LVFN, LVFN, 1
            Else
               Break
         }
         NewFileName = %LVFN%
      }
      GuiControlGet, Sep,, Separator
      If Sep = 2
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, ., All
      }
      Else If Sep = 3
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, _, All
      }
      NewFileName = %NewFileName%.%LVFileExt%
      LV_Modify(RowNumber, Col1, LVFileName, NewFileName, LVFileDir, LVFileExt, SeriesIDNo)
      LV_ModifyCol()
      LV_ModifyCol(4, 60)
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      LVFileName =
      LVPrevFN =
      ShowArray1 =
      OutputArray1 =
      OutputArray2 =
      OutputArray3 =
      SeasonNumber =
      EpisodeNumber =
      EpisodeTitle =
      OriginalTitle =
      RowNumber := RowNumber + 1
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the filename
      If not LVFileName  ; Break the loop at the end of the list
         Break
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file directory
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
   }
   ControlSetText, Edit1, Finished querying TheTVDB.com. Ready to rename..., %WindowTitle%
   Return
}

ButtonRename:
   LV_GetText(LVNewFileName, 1, 2)
   If not LVNewFileName
   {
      GuiControlGet, DirectRename,, Rename
      If DirectRename = 1
      {
         GetTVDBInfo()
      }
      Else
      {
         Gui +OwnDialogs
         MsgBox, 0,Fatal Error!, You must scan your files against TheTVDB.com before you rename them!
         Return
      }
   }
   Loop
   {
      RowNumber = %A_Index%
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the original filename
      LV_GetText(LVNewFileName, RowNumber, 2)  ; Get the new filename
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file dir
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
      If not LVNewFileName  ; Break the loop at the end of the list
         Break
      ControlSetText, Edit1, Renaming "%LVFileName%" to "%LVNewFileName%", %WindowTitle%
      FileMove, %LVFileDir%\%LVFileName%, %LVFileDir%\%LVNewFileName%  ; Finally, rename it
      LV_Modify(RowNumber, Col1, LVNewFileName, "", LVFileDir, LVFileExt, SeriesIDNo)
   }
   RenameCount := RowNumber - 1
   ControlSetText, Edit1, Finished renaming files! Refreshing file list., %WindowTitle%
   GuiControlGet, ClearIt,, Clear
   If ClearIt = 1
   {
      ClearList()
   }
   Else
   {
      GuiControl, +Redraw, MyListView  ; Turn redrawing back on
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   }
   ControlSetText, Edit1, Finished Successfully!, %WindowTitle%
   Return

;Provide a filename (greek.s01e01.original.title.avi) and this should return the original title (in case a title isn't found)
GetTitle(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Loop
   {
      StringRight, LastChar, ShowTitle, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Loop
   {
      StringLeft, FirstChar, ShowTitle, 1
      If (FirstChar = "-" or FirstChar = " ")
         StringTrimLeft, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Return ShowTitle
}

;Provide a filename (s01e01.avi or greek.s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetShowName(FileName, FileDir)
{
   TempName := GetName(FileName)
   If not TempName
      TempName := GetNameFromDir(FileName, FileDir)
   Return TempName
}
;Provide a filename (greek.s01e01.avi) and this should return the show name
GetName(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Loop
   {
      StringRight, LastChar, ShowName, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowName, ShowName, 1
      Else
         Break
   }
   If SeasonAndEpisodePos > -1
      Return ShowName
   Else
   {
      Return FileName
   }
}

;Provide a filename (s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetNameFromDir(FileName, FileDir)
{
   Directory = %FileDir% - %FileName%
   SplitPath , Directory, NewFileName, dir, ext, name
   Return GetName(NewFileName)
}

;Provide a filename (greek.s01e01.avi) and this should return the season number
GetSeasonNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }

   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringTrimRight, SeasonNumber, SeasonAndEpisode, 2
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Return SeasonNumber
}

;Provide a filename (greek.s01e01.avi) and this should return the episode number
GetEpisodeNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?.?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringRight, EpisodeNumber, SeasonAndEpisode, 2
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Return EpisodeNumber
}

;============================== RELEASE ==========================
GetRelease(FileName)
{
   ; Stores the source, codec and group to use latter
   RELEASE =

   ;~ GET IF IS REPACK ou PROPER
   StringGetPos, REPACK, FileName, repack, R, 4
   If (REPACK <> -1) {
      RELEASE = %RELEASE%REPACK.
  }
   StringGetPos, PROPER, FileName, proper, R, 4
   If (PROPER <> -1) {
      RELEASE = %RELEASE%PROPER.
}
   
   ;~ GET SOURCE!
   StringGetPos, Source_720p, FileName, 720p, R, 4
   If (Source_720p <> -1) {
      RELEASE = %RELEASE%720p.
  }
   StringGetPos, Source_HDTV, FileName, HDTV, R, 4
   If (Source_HDTV >= 7) {
      RELEASE = %RELEASE%HDTV.
  }
   StringGetPos, Source_DVDRip, FileName, dvdrip, R, 4
   If (Source_DVDRip >= 7) {
      RELEASE = %RELEASE%DVDRip.
  }
    
   ;~ GET CODEC!
   StringGetPos, CODEC_XVID, FileName, xvid, R, 4
   If (CODEC_XVID <> -1) {
      RELEASE = %RELEASE%XviD-
  }
   StringGetPos, CODEC_X264, FileName, x264, R, 4
   If (CODEC_X264 <> -1) {
      RELEASE = %RELEASE%x264-
  }
   StringGetPos, CODEC_DIVX, FileName, divx, R, 4
   If (CODEC_DIVX <> -1) {
      RELEASE = %RELEASE%DivX-
  }
  
  ;nasty code but that works.... :S
  OFFSET := 1+CODEC_XVID+CODEC_DIVX+5
  StringRight, GROUP, FileName, StrLen(FileName)-OFFSET
  StringTrimRight, GROUP, GROUP, 4
  RELEASE = %RELEASE%%GROUP%
  
;~   MsgBox, DVDRip = %Source_DVDRip%`nXviD = %CODEC_XVID%`nDivX = %CODEC_DIVX%`nOFFSET = %OFFSET%`nGROUP = %GROUP%`nSUFFIX = %SUFFIX%
  
   Return RELEASE
}
;=================================================================


;Provide a show name (greek) and it will return the TVDB data for the show name (Greek) and series ID (80301) as a CSV (Greek,80301)
GetTVDBShows(ShowName)
{
   Global SeriesName, SeriesIDNum
   IfExist, Series_ID.xml
      FileDelete, Series_ID.xml
   UrlDownloadToFile, http://www.thetvdb.com/api/GetSeries.php?seriesname="%ShowName%", Series_ID.xml
   XPath_Load(xml, "Series_ID.xml")
   FileDelete, Series_ID.xml
   SeriesNameNodeCount := XPath(xml, "/Data/Series/SeriesName/Count()")
   If SeriesNameNodeCount = 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
   }
   Else If SeriesNameNodeCount > 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
      SeriesOverview := XPath(xml, "/Data/Series/Overview/text()")
      StringSplit, OutputArraySID, SeriesIDNum, `,
      StringSplit, OutputArraySN, SeriesName, `,
      StringSplit, OutputArraySO, SeriesOverview, `,
      Gui, 2: Default
      Gui, 2: +ToolWindow +Owner1
      Gui, 2: Add, ListView, xm r15 w600, Show name|Show description|TVDB series ID no.
      Gui, 2: Add, Text, y+7
      Gui, 2: Add, Button, x+475, Select
      Gui, 2: Add, Button, x+15, Cancel
      Loop, %SeriesNameNodeCount%
      {
         SeriesIDNum := OutputArraySID%A_Index%
         SeriesName := OutputArraySN%A_Index%
         SeriesOverview := OutputArraySO%A_Index%
         IfInString, SeriesOverview, ,`;
            StringReplace, SeriesOverview, SeriesOverview, ,`;, `,, All
         If not SeriesOverview
            SeriesOverview = (No show description provided)
         LV_Add(Row, SeriesName, SeriesOverview, SeriesIDNum)
         LV_ModifyCol()
         LV_ModifyCol(3, 110)
         SeriesIDNum =
         SeriesName =
         SeriesOverview =
      }
      Gui, 2: Show,,Which Show?
      Gui, 1: Default
      WinWaitClose, Which Show?
   }
   If not SeriesName
      SeriesName = %ShowName%
   J = %SeriesName%,%SeriesIDNum%
   ShowName =
   SeriesIDNum =
   SeriesName =
   SeriesOverview =
   Return J
}

   ;Labels required for Gui 2
   2GuiEscape:
   2GuiClose:
   2ButtonCancel:
   {
      Gui, 2: Destroy
      Return
   }
   2ButtonSelect:
   {
      FocusedRowNumber := LV_GetNext(0, "F")
      If not FocusedRowNumber
      {
         MsgBox, 0, Warning!, You must select a show or press cancel!
         Return
      }
      LV_GetText(SeriesName, FocusedRowNumber, 1)
      LV_GetText(SeriesIDNum, FocusedRowNumber, 3)
      FocusedRowNumber =
      Gui, 2: Destroy
      Return
   }

;Provide the "TVDB series ID", "season number" and "episode number" and this will return the TVDB season number, episode number, and episode title as a CSV (3,1,The Day After)
GetTVDBEpisodes(SeriesIDNum, SeasonNum, EpisodeNum, ShowName)
{
   GuiControlGet, CacheInfo,, Cache
   FileName = data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml
   If CacheInfo = 1
   {
      IfNotExist, FileName
         UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, %FileName%
      If not XPath_Load(xml, FileName)
         Msgbox, 48, [XPath]  , Load of %FileName% Failed!
   }
   Else
   {
      IfExist, Episode_Info.xml
         FileDelete, Episode_Info.xml
      UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, Episode_Info.xml
      If not XPath_Load(xml, "Episode_Info.xml")
         Msgbox, 48, [XPath]  , Load of "Episode_Info.xml" Failed!
      FileDelete, Episode_Info.xml
   }
   SeasonNumber := XPath(xml, "/Data/Episode/SeasonNumber/text()")
   EpisodeNumber := XPath(xml, "/Data/Episode/EpisodeNumber/text()")
   EpisodeTitle := XPath(xml, "/Data/Episode/EpisodeName/text()")
   If not EpisodeTitle
   {
      IfExist, %FileName%
         FileDelete, %FileName%
   }
   If not SeasonNumber
      SeasonNumber = %SeasonNum%
   If not EpisodeNumber
      EpisodeNumber = %EpisodeNum%
   K = %SeasonNumber%,%EpisodeNumber%,%EpisodeTitle%
   SeasonNumber =
   EpisodeNumber =
   EpisodeTitle =
   Return K
}

;Pads a variable with a preceeding zero
PadZero(VarToPad)
{
   VarToPadLen := StrLen(VarToPad)
   If VarToPadLen = 1
      VarToPad = 0%VarToPad%
   VarToPadLen =
   Return VarToPad
}

/*
>>>>CHANGELOG<<<<

1.0 (10-5-2009)
   - Original script
1.1 (10-6-2009)
   - Fixed a bug with adding a single file where the file would not be renamed
   -- Forgot to define the TargetFile's TargetFolder when updating the GUI
1.2 (10-13-2009)
   - Fixed another bug with adding a single file where the file would not be renamed
   - Fixed a bug with parsing the series overview where the non-comma delimiter (,) would be displayed
   -- Titans XPath replaces commas with "," (no quotes)
1.30 (11-8-2009)
   - Added a new method of selecting a show name when multiple results are returned
   -- Secondary ListView gui is displayed allowing the user to select the correct show
   - Script now compares the current filename against the previous filename (filenames converted to UPPERCASE)
   -- Drasitically reduces the number of requests against the TVDB and speeds everything up when renaming multiple shows all with the same name
   - Added a check and replace system for invalid filename characters
   -- StringReplace these chars \ / : * ? "" < > | with descriptive replacements
   - Fixed a bug in the GetEpisodeNumber() function
   -- A single digit season number would sometimes cause too few of the episode numbers to be returned when SeasonAndEpisode contained (e|x)
   - Tweaked GetSeasonNumber() and GetEpisodeNumber() to more reliably return correct data
   -- StringGetPos and String(Left|Right) were swapped for StringSplit to more predictably split SeasonAndEpisode regardless of (e|x) char location
   - Added a check to make sure files have a new filename in the preview column before renaming
   - Added a static window title which may be needed for various features in the future as well as making things look nicer :P
1.40 (11-15-2009)
   - Added status bar
   - Fixed a bug where a file would not have a preceeding zero added if no info was found on TheTVDB by adding PadZero() function
   -- Preceeding zero was only added in GetTVDBEpisodes() which means that if SeriesIDNo was blank (i.e. no info pulled from TheTVDB) the Season and Episode number would not get padded

1.40a (12-11-2009) by Pacheco
   - Added an option to preserve the information of [Source.Codec-Group] if present

1.41 (12-10-2009)
   - Fixed a serious bug where no results would be returned for ShowName inside the GetTVDBShows function would return the first result infinitely
   -- Had to clear the SeriesName var at the end of the function so that on the next loop iteration the correct var contents would exist
1.42 (12-11-2009)
   - Fixed a minor bug where the second time the GetTVDBInfo button was pressed, the first item in the list view would not actually be rescanned
   -- Had to clear the UpperPrevSN and PrevShowName vars so that the second time the button was pressed it would compare the correct names

>>>>Paradox Changelog<<<<

1.42.1 (2-5-2010)
   - Added new checkbox and supporting code that allows you to save the xmls downloaded in data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml to make future renames quicker.
   -- Added check that determines if you are connected to the internet and thetvdb.com is reachable
   - Added better handling for previous showname
   -- Clean up some code that caused shows to use the previous renamed file settings wrongly
   - Renamed GetShowName(FileName) to GetName(FileName) and added new GetShowName(FileName, FileDir) to implement the following feature without requiring big changes to existing scripts.
   -- Added GetNameFromDir(FileName, Directory) to handle using the directory name as the showname, in case you are renaming files named 01x01 - title.avi the directory must, of course, be named as the show name.
   - Added debug message box if the episode info xml can't be read for some reason.
   -- Fixed Bug where first file would be properly renamed "Castle [2009] - 01x01 - Title" and the rest would be improperly renamed Castle - 01x02 - Title"
   - I am planning to add more features including using the API properly to download All the Episode information for a show and cache it locally/update on a regular basis.
   -- Also Planning to add a feature that will recognize when you are missing episodes from a season and either create a .txt file with the show name, season, episode, and title in the directory, or, keep track of it in a log file of some sort.
1.42.2 (2-5-2010)
   - Added support for choosing if you want to use spaces, periods or underscores as separators
   -- Added A checkbox that allows you to clear the list after renaming, so you don't have to click the button :)
   - Moved some things on the interface around a bit to accommodate previous features.
   -- Fixed an error where the list of files in the directory were being displayed after renaming instead of the previous items, renamed.
   - Added an option that allows you to click on rename and have it do the TVDB lookup and rename all in one (Be careful with this option)
   -- Fix up a few other small bugs
1.42.3 (2-5-2010)
   - Remove Some debug code I forgot to remove before.
   -- Save original title (The part between the 01x01 and the extension), if any to be added back later if no official title is found.
   - Implement better way of refreshing the display after a rename.
1.42.4 (2-5-2010)
   - Fix Typo that would keep files with no . in them at all from being renamed properly. (Exists in original 1.42 script as well)
1.42.5 (2-6-2010)
   - Fix renaming files with no Season and Episode Number (use original filename)
1.42.5a (2-8-2010)
  - Re-added code to read/keep the info of the release version... [by Pacheco]
*/
[/quote]

Delusion
  • Members
  • 272 posts
  • Last active: Jul 13 2014 09:04 PM
  • Joined: 16 Jul 2008
yeah having the release group at the end is very usefull for the people who download or create subtitles
many thanks :D
QuickSubs | Popcorn Movie Catalog
All my scripts are just in AutoHotkey v1.0.48.05

Paradox
  • Members
  • 24 posts
  • Last active: Mar 05 2013 04:15 PM
  • Joined: 05 Feb 2010
This is another reason I'm splitting off with my new version, which will be almost unrecognizable, when it's done.... I don't care about the release group, or codec or any of that (and most of mine are missing that info long ago anyway) they are just for myself and some family/friends to watch, but, I like to know when I'm missing an episode or something, so, I'm almost completely rewriting this so it keeps a database of what shows you have, and when you run the renamer part, it will notify you of missing episodes (unless it's a multipart episode, and you seem to only have the first, and you have checked the option) It's also going to be handy for taking care of some series I have where some of them are in aired order, and some are in DVD order.... You'll be able to switch the numbering system and rename by either system... Basically, I'm creating a pseudo front end for this renamer...


With all of that being said, I will probably go ahead and incorporate your code in for those who will want this feature.

Paradox
  • Members
  • 24 posts
  • Last active: Mar 05 2013 04:15 PM
  • Joined: 05 Feb 2010
I know I said the next version would be a great departure, but, I had to release one more version before that, because, while adding Pacheco's changes, I noticed that we both have a closing curly brace in the wrong place, causing some code to never be run.... so, I figured I'd get one more revision out there before the big one.

P.S. Pacheco, your code has a bug, which I plan to fix for my new version, but that is is still bugged in this one, if you run the renamer on a file that doesn't have the release info in its name, it doubles the Episode Number and the title, instead of just leaving the file named as it was to begin with....


Edit, I found out why, it's being returned as the group name......
1.42.6

; SceneRenamer by strictlyfocused02
; Modified By Paradox iamparadox at netscape.net
; AutoHotkey Version: 1.0.48+
;
; Script Function:
;   Extracts season and episode info from the filename
;   and uses that to retreive info from TheTVDB.com to rename
;   the file to a user-specified format

;   Special thanks to TheTVDB.com
;   This project would not have been possible without their top-notch API

#Include xpath.ahk

#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%

WindowTitle = SceneRenamer v1.42.6 Modified By Paradox

Gui, Add, Button, Default gButtonAddFolder, &Add folder
Gui, Add, Button, x+20 gButtonAddFile, &Add file
Gui, Add, Button, x+20 gButtonClear, &Clear list
Gui, Add, Text, x+150 w50, Status:
Gui, Add, Edit, +ReadOnly x+0 w400, Waiting for user to add file(s)...

Gui, Add, ListView, xm r26 w800 -ReadOnly vMyListView, Name|Preview|Directory|Extension|TVDB Series ID No

Gui, Add, DropDownList, w300 AltSubmit vNameScheme, (Show Title) - (SS)x(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title)|(SS)x(EE) - Episode Title||S(SS)E(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title) - (Release)
Gui, Add, DropDownList, x+10 w180 AltSubmit vSeparator, Use spaces as separators|Use.periods.as.separators||Use_underscores_as_separators
Gui, Add, Button, x+100 gButtonGetTVDBInfo, &Get TVDB Info
Gui, Add, Button, x+10 gButtonRename, &Rename files

Gui, Add, Checkbox, xm Checked vZero, Pad with zero?
Gui, Add, Checkbox, x+10 Checked vCache, Cache Episode Info?
Gui, Add, Checkbox, x+10 vRename, Allow Direct Rename?
Gui, Add, Checkbox, x+10 Checked vClear, Clear List after Rename?

Menu, MyContextMenu, Add, Open, ContextOpenFile
Menu, MyContextMenu, Add, Properties, ContextProperties
Menu, MyContextMenu, Add, Remove item, ContextRemoveItem

Gui, Show,, %WindowTitle%
Return

GuiClose:
   ExitApp

GuiContextMenu:
   If A_GuiControl <> MyListView
       Return
   Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
   Return

ContextOpenFile:
ContextProperties:
   FocusedRowNumber := LV_GetNext(0, "F")
   If not FocusedRowNumber
      return
   LV_GetText(FileName, FocusedRowNumber, 1)
   LV_GetText(FileDir, FocusedRowNumber, 3)
   IfInString A_ThisMenuItem, Open
      Run %FileDir%\%FileName%,, UseErrorLevel
   Else
      Run Properties "%FileDir%\%FileName%",, UseErrorLevel
   If ErrorLevel
      MsgBox Could not perform requested action on "%FileDir%\%FileName%".
   return

ContextRemoveItem:
   RowNumber = 0
   Loop
   {
      RowNumber := LV_GetNext(RowNumber - 1)
      If not RowNumber
           Break
      LV_Delete(RowNumber)
   }
   Return

ButtonAddFolder:
   Gui +OwnDialogs
   FileSelectFolder, TargetFolder,, 3, Select a folder:
   If not TargetFolder  ; The user pressed cancel
       Return
   StringRight, LastChar, TargetFolder, 1
   If LastChar = \
      StringTrimRight, TargetFolder, TargetFolder, 1
   GuiControl, -Redraw, MyListView ; Improves performance by disabling redrawing
   Loop %TargetFolder%\*.*
   {
      FileName := A_LoopFileFullPath
      SplitPath, FileName,,, FileExt  ; Get the file extension
      If FileExt in avi,mkv,mp4,srt
         LV_Add(Row, A_LoopFileName, "", A_LoopFileDir, FileExt)
   }
   GuiControl, +Redraw, MyListView  ; Turn redrawing back on
   LV_ModifyCol()  ; Resize each column to fit
   LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
   LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
   LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   ControlSetText, Edit1, Added folder %TargetFolder%, %WindowTitle%
   Return

ButtonAddFile:
   Gui +OwnDialogs
   FileSelectFile, TargetFile
   If not TargetFile
      Return
   SplitPath, TargetFile,,, FileExt  ; Get the file extension
   If FileExt in avi,mkv,mp4,srt
   {
      SplitPath, TargetFile, FileName, TargetFolder
      LV_Add(Row, FileName, "", TargetFolder, FileExt)
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      ControlSetText, Edit1, Added file %TargetFile%..., %WindowTitle%
   }
   Return

ButtonClear:
   ClearList()
   Return

ClearList()
{
   TargetFolder =
   TargetFile =
   LV_Delete()
   ControlSetText, Edit1, Cleared Listview, %WindowTitle%
   Return
}

InternetCheckConnection(Url="",FIFC=1) {
   Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0)
}

ButtonGetTVDBInfo:
   GetTVDBInfo()
   Return

GetTVDBInfo()
{
   LV_GetText(LVFileName, 1, 1)
   If not LVFileName
   {
      Gui +OwnDialogs
      MsgBox, 0,Fatal Error!, You must select a file or folder before connecting to TheTVDB.com!
      Return
   }
   LV_GetText(LVFileDir, 1, 3)  ; Get the file directory
   LV_GetText(LVFileExt, 1, 4)  ; Get the file extension

   URL := "http://www.thetvdb.com"

   If not InternetCheckConnection(URL)
   {
      Msgbox, 48, [%URL%]  , Connection Failed!
      Return
   }

   RowNumber = 1
   ShowInfo := GetTVDBShows(GetShowName(LVFileName, LVFileDir))
   StringSplit, ShowArray, ShowInfo, `,
   ShowName = %ShowArray1%
   SeriesIDNo = %ShowArray2%
   Loop
   {
      Gui +OwnDialogs
      If A_Index > 1
      {
         LV_GetText(LVPrevFN, RowNumber - 1, 1)  ; Get the preceeding rows filename
         LV_GetText(LVPrevFD, RowNumber - 1, 3)  ; Get the preceeding rows file directory
         PrevShowName := GetShowName(LVPrevFN, LVPrevFD)
         StringUpper, UpperPrevSN, PrevShowName

         NewShowName := GetShowName(LVFileName, LVFileDir)
         StringUpper, UpperCurrSN, NewShowName
         If UpperCurrSN <> %UpperPrevSN%
         {
            ShowInfo := GetTVDBShows(NewShowName)
            StringSplit, ShowArray, ShowInfo, `,
            ShowName = %ShowArray1%
            SeriesIDNo = %ShowArray2%
         }
      }
      UpperPrevSN =
      SeasonNumber := GetSeasonNumber(LVFileName)
      EpisodeNumber := GetEpisodeNumber(LVFileName)
      OriginalTitle := GetTitle(LVFileName)
      ReleaseInfo := GetRelease(LVFileName)
      ControlSetText, Edit1, Processing "%ShowName%" Season "%SeasonNumber%" Episode "%EpisodeNumber%", %WindowTitle%
      If SeriesIDNo
      {
         IfInString, ShowName, %A_Space%
            StringReplace, SafeShowName, ShowName, %A_Space%, _, All
         Else
            SafeShowName = %ShowName%
         EpisodeInfo := GetTVDBEpisodes(SeriesIDNo, SeasonNumber, EpisodeNumber, SafeShowName)
         StringSplit, OutputArray, EpisodeInfo, `,
         SeasonNumber = %OutputArray1%
         EpisodeNumber = %OutputArray2%
         EpisodeTitle = %OutputArray3%
         If EpisodeTitle
         {
            StringReplace, EpisodeTitle, EpisodeTitle, \, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, /, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, :, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, *, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, ?,, All
            StringReplace, EpisodeTitle, EpisodeTitle, ", ', All
            StringReplace, EpisodeTitle, EpisodeTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, |, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, &`#44;, `,, All
            StringReplace, EpisodeTitle, EpisodeTitle, &, &, All
         }
         If OriginalTitle
         {
            StringReplace, OriginalTitle, OriginalTitle, \, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, /, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, :, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, *, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, ?,, All
            StringReplace, OriginalTitle, OriginalTitle, ", ', All
            StringReplace, OriginalTitle, OriginalTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, |, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, &`#44;, `,, All
            StringReplace, OriginalTitle, OriginalTitle, &, &, All
         }
         GuiControlGet, PadZero,, Zero
         If PadZero = 1
         {
            SeasonNumber := PadZero(SeasonNumber)
            EpisodeNumber := PadZero(EpisodeNumber)
         }
         GuiControlGet, NamingScheme,, NameScheme
         If NamingScheme = 1
         {
            If EpisodeTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber%
         }
         Else if NamingScheme = 2
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 3
         {
            If EpisodeTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %SeasonNumber%x%EpisodeNumber%
         }
         Else If NamingScheme = 4
         {
            If EpisodeTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 5
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle% - %ReleaseInfo%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle% - %ReleaseInfo%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %ReleaseInfo%
         }
      }
      Else
      {
         StringReplace, LVFN, LVFileName, \, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, /, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, :, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, *, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, ?,, All
         StringReplace, LVFN, LVFN, ", ', All
         StringReplace, LVFN, LVFN, <, %A_Space%less than%A_Space%, All
         StringReplace, LVFN, LVFN, >, %A_Space%greater than%A_Space%, All
         StringReplace, LVFN, LVFN, |, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, &`#44;, `,, All
         StringReplace, LVFN, LVFN, &, &, All
         StringTrimRight, LVFN, LVFN, 4
         Loop
         {
            StringRight, LastChar, LVFN, 1
            If (LastChar = "-" or LastChar = " ")
               StringTrimRight, LVFN, LVFN, 1
            Else
               Break
         }
         NewFileName = %LVFN%
      }
      GuiControlGet, Sep,, Separator
      If Sep = 2
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, ., All
      }
      Else If Sep = 3
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, _, All
      }
      NewFileName = %NewFileName%.%LVFileExt%
      LV_Modify(RowNumber, Col1, LVFileName, NewFileName, LVFileDir, LVFileExt, SeriesIDNo)
      LV_ModifyCol()
      LV_ModifyCol(4, 60)
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      LVFileName =
      LVPrevFN =
      ShowArray1 =
      OutputArray1 =
      OutputArray2 =
      OutputArray3 =
      SeasonNumber =
      EpisodeNumber =
      EpisodeTitle =
      OriginalTitle =
      RowNumber := RowNumber + 1
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the filename
      If not LVFileName  ; Break the loop at the end of the list
         Break
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file directory
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
   }
   ControlSetText, Edit1, Finished querying TheTVDB.com. Ready to rename..., %WindowTitle%
   Return
}

ButtonRename:
{
   LV_GetText(LVNewFileName, 1, 2)
   If not LVNewFileName
   {
      GuiControlGet, DirectRename,, Rename
      If DirectRename = 1
      {
         GetTVDBInfo()
      }
      Else
      {
         Gui +OwnDialogs
         MsgBox, 0,Fatal Error!, You must scan your files against TheTVDB.com before you rename them!
         Return
      }
   }
   Loop
   {
      RowNumber = %A_Index%
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the original filename
      LV_GetText(LVNewFileName, RowNumber, 2)  ; Get the new filename
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file dir
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
      If not LVNewFileName  ; Break the loop at the end of the list
         Break
      ControlSetText, Edit1, Renaming "%LVFileName%" to "%LVNewFileName%", %WindowTitle%
      FileMove, %LVFileDir%\%LVFileName%, %LVFileDir%\%LVNewFileName%  ; Finally, rename it
      LV_Modify(RowNumber, Col1, LVNewFileName, "", LVFileDir, LVFileExt, SeriesIDNo)
   }
   RenameCount := RowNumber - 1
   ControlSetText, Edit1, Finished renaming files! Refreshing file list., %WindowTitle%
   GuiControlGet, ClearIt,, Clear
   If ClearIt = 1
   {
      ClearList()
   }
   Else
   {
      GuiControl, +Redraw, MyListView  ; Turn redrawing back on
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   }
   ControlSetText, Edit1, Finished Successfully!, %WindowTitle%
   Return
}

;Provide a filename (greek.s01e01.original.title.avi) and this should return the original title (in case a title isn't found)
GetTitle(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Loop
   {
      StringRight, LastChar, ShowTitle, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Loop
   {
      StringLeft, FirstChar, ShowTitle, 1
      If (FirstChar = "-" or FirstChar = " ")
         StringTrimLeft, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Return ShowTitle
}

;Provide a filename (s01e01.avi or greek.s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetShowName(FileName, FileDir)
{
   TempName := GetName(FileName)
   If not TempName
      TempName := GetNameFromDir(FileName, FileDir)
   Return TempName
}
;Provide a filename (greek.s01e01.avi) and this should return the show name
GetName(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Loop
   {
      StringRight, LastChar, ShowName, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowName, ShowName, 1
      Else
         Break
   }
   If SeasonAndEpisodePos > -1
      Return ShowName
   Else
   {
      Return FileName
   }
}

;Provide a filename (s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetNameFromDir(FileName, FileDir)
{
   Directory = %FileDir% - %FileName%
   SplitPath , Directory, NewFileName, dir, ext, name
   Return GetName(NewFileName)
}

;Provide a filename (greek.s01e01.avi) and this should return the season number
GetSeasonNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }

   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringTrimRight, SeasonNumber, SeasonAndEpisode, 2
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Return SeasonNumber
}

;Provide a filename (greek.s01e01.avi) and this should return the episode number
GetEpisodeNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?.?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringRight, EpisodeNumber, SeasonAndEpisode, 2
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Return EpisodeNumber
}

;============================== RELEASE ==========================
GetRelease(FileName)
{
   ; Stores the source, codec and group to use latter
   RELEASE =

   ;~ GET IF IS REPACK ou PROPER
   StringGetPos, REPACK, FileName, repack, R, 4
   If (REPACK <> -1) {
      RELEASE = %RELEASE%REPACK.
  }
   StringGetPos, PROPER, FileName, proper, R, 4
   If (PROPER <> -1) {
      RELEASE = %RELEASE%PROPER.
}
   
   ;~ GET SOURCE!
   StringGetPos, Source_720p, FileName, 720p, R, 4
   If (Source_720p <> -1) {
      RELEASE = %RELEASE%720p.
  }
   StringGetPos, Source_HDTV, FileName, HDTV, R, 4
   If (Source_HDTV >= 7) {
      RELEASE = %RELEASE%HDTV.
  }
   StringGetPos, Source_DVDRip, FileName, dvdrip, R, 4
   If (Source_DVDRip >= 7) {
      RELEASE = %RELEASE%DVDRip.
  }
   
   ;~ GET CODEC!
   StringGetPos, CODEC_XVID, FileName, xvid, R, 4
   If (CODEC_XVID <> -1) {
      RELEASE = %RELEASE%XviD-
  }
   StringGetPos, CODEC_X264, FileName, x264, R, 4
   If (CODEC_X264 <> -1) {
      RELEASE = %RELEASE%x264-
  }
   StringGetPos, CODEC_DIVX, FileName, divx, R, 4
   If (CODEC_DIVX <> -1) {
      RELEASE = %RELEASE%DivX-
  }
 
  ;nasty code but that works.... :S
  OFFSET := 1+CODEC_XVID+CODEC_DIVX+5
  StringRight, GROUP, FileName, StrLen(FileName)-OFFSET
  StringTrimRight, GROUP, GROUP, 4
  RELEASE = %RELEASE%%GROUP%
 
;~   MsgBox, DVDRip = %Source_DVDRip%`nXviD = %CODEC_XVID%`nDivX = %CODEC_DIVX%`nOFFSET = %OFFSET%`nGROUP = %GROUP%`nSUFFIX = %SUFFIX%
 
   Return RELEASE
}
;=================================================================


;Provide a show name (greek) and it will return the TVDB data for the show name (Greek) and series ID (80301) as a CSV (Greek,80301)
GetTVDBShows(ShowName)
{
   Global SeriesName, SeriesIDNum
   IfExist, Series_ID.xml
      FileDelete, Series_ID.xml
   UrlDownloadToFile, http://www.thetvdb.com/api/GetSeries.php?seriesname="%ShowName%", Series_ID.xml
   XPath_Load(xml, "Series_ID.xml")
   FileDelete, Series_ID.xml
   SeriesNameNodeCount := XPath(xml, "/Data/Series/SeriesName/Count()")
   If SeriesNameNodeCount = 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
   }
   Else If SeriesNameNodeCount > 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
      SeriesOverview := XPath(xml, "/Data/Series/Overview/text()")
      StringSplit, OutputArraySID, SeriesIDNum, `,
      StringSplit, OutputArraySN, SeriesName, `,
      StringSplit, OutputArraySO, SeriesOverview, `,
      Gui, 2: Default
      Gui, 2: +ToolWindow +Owner1
      Gui, 2: Add, ListView, xm r15 w600, Show name|Show description|TVDB series ID no.
      Gui, 2: Add, Text, y+7
      Gui, 2: Add, Button, x+475, Select
      Gui, 2: Add, Button, x+15, Cancel
      Loop, %SeriesNameNodeCount%
      {
         SeriesIDNum := OutputArraySID%A_Index%
         SeriesName := OutputArraySN%A_Index%
         SeriesOverview := OutputArraySO%A_Index%
         IfInString, SeriesOverview, ,`;
            StringReplace, SeriesOverview, SeriesOverview, ,`;, `,, All
         If not SeriesOverview
            SeriesOverview = (No show description provided)
         LV_Add(Row, SeriesName, SeriesOverview, SeriesIDNum)
         LV_ModifyCol()
         LV_ModifyCol(3, 110)
         SeriesIDNum =
         SeriesName =
         SeriesOverview =
      }
      Gui, 2: Show,,Which Show?
      Gui, 1: Default
      WinWaitClose, Which Show?
   }
   If not SeriesName
      SeriesName = %ShowName%
   J = %SeriesName%,%SeriesIDNum%
   ShowName =
   SeriesIDNum =
   SeriesName =
   SeriesOverview =
   Return J
}

   ;Labels required for Gui 2
   2GuiEscape:
   2GuiClose:
   2ButtonCancel:
   {
      Gui, 2: Destroy
      Return
   }
   2ButtonSelect:
   {
      FocusedRowNumber := LV_GetNext(0, "F")
      If not FocusedRowNumber
      {
         MsgBox, 0, Warning!, You must select a show or press cancel!
         Return
      }
      LV_GetText(SeriesName, FocusedRowNumber, 1)
      LV_GetText(SeriesIDNum, FocusedRowNumber, 3)
      FocusedRowNumber =
      Gui, 2: Destroy
      Return
   }

;Provide the "TVDB series ID", "season number" and "episode number" and this will return the TVDB season number, episode number, and episode title as a CSV (3,1,The Day After)
GetTVDBEpisodes(SeriesIDNum, SeasonNum, EpisodeNum, ShowName)
{
   GuiControlGet, CacheInfo,, Cache
   FileName = data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml
   If CacheInfo = 1
   {
      IfNotExist, FileName
         UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, %FileName%
      If not XPath_Load(xml, FileName)
         Msgbox, 48, [XPath]  , Load of %FileName% Failed!
   }
   Else
   {
      IfExist, Episode_Info.xml
         FileDelete, Episode_Info.xml
      UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, Episode_Info.xml
      If not XPath_Load(xml, "Episode_Info.xml")
         Msgbox, 48, [XPath]  , Load of "Episode_Info.xml" Failed!
      FileDelete, Episode_Info.xml
   }
   SeasonNumber := XPath(xml, "/Data/Episode/SeasonNumber/text()")
   EpisodeNumber := XPath(xml, "/Data/Episode/EpisodeNumber/text()")
   EpisodeTitle := XPath(xml, "/Data/Episode/EpisodeName/text()")
   If not EpisodeTitle
   {
      IfExist, %FileName%
         FileDelete, %FileName%
   }
   If not SeasonNumber
      SeasonNumber = %SeasonNum%
   If not EpisodeNumber
      EpisodeNumber = %EpisodeNum%
   K = %SeasonNumber%,%EpisodeNumber%,%EpisodeTitle%
   SeasonNumber =
   EpisodeNumber =
   EpisodeTitle =
   Return K
}

;Pads a variable with a preceeding zero
PadZero(VarToPad)
{
   VarToPadLen := StrLen(VarToPad)
   If VarToPadLen = 1
      VarToPad = 0%VarToPad%
   VarToPadLen =
   Return VarToPad
}

/*
>>>>CHANGELOG<<<<

1.0 (10-5-2009)
   - Original script
1.1 (10-6-2009)
   - Fixed a bug with adding a single file where the file would not be renamed
   -- Forgot to define the TargetFile's TargetFolder when updating the GUI
1.2 (10-13-2009)
   - Fixed another bug with adding a single file where the file would not be renamed
   - Fixed a bug with parsing the series overview where the non-comma delimiter (,) would be displayed
   -- Titans XPath replaces commas with "," (no quotes)
1.30 (11-8-2009)
   - Added a new method of selecting a show name when multiple results are returned
   -- Secondary ListView gui is displayed allowing the user to select the correct show
   - Script now compares the current filename against the previous filename (filenames converted to UPPERCASE)
   -- Drasitically reduces the number of requests against the TVDB and speeds everything up when renaming multiple shows all with the same name
   - Added a check and replace system for invalid filename characters
   -- StringReplace these chars \ / : * ? "" < > | with descriptive replacements
   - Fixed a bug in the GetEpisodeNumber() function
   -- A single digit season number would sometimes cause too few of the episode numbers to be returned when SeasonAndEpisode contained (e|x)
   - Tweaked GetSeasonNumber() and GetEpisodeNumber() to more reliably return correct data
   -- StringGetPos and String(Left|Right) were swapped for StringSplit to more predictably split SeasonAndEpisode regardless of (e|x) char location
   - Added a check to make sure files have a new filename in the preview column before renaming
   - Added a static window title which may be needed for various features in the future as well as making things look nicer :P
1.40 (11-15-2009)
   - Added status bar
   - Fixed a bug where a file would not have a preceeding zero added if no info was found on TheTVDB by adding PadZero() function
   -- Preceeding zero was only added in GetTVDBEpisodes() which means that if SeriesIDNo was blank (i.e. no info pulled from TheTVDB) the Season and Episode number would not get padded

1.40a (12-11-2009) by Pacheco
   - Added an option to preserve the information of [Source.Codec-Group] if present

1.41 (12-10-2009)
   - Fixed a serious bug where no results would be returned for ShowName inside the GetTVDBShows function would return the first result infinitely
   -- Had to clear the SeriesName var at the end of the function so that on the next loop iteration the correct var contents would exist
1.42 (12-11-2009)
   - Fixed a minor bug where the second time the GetTVDBInfo button was pressed, the first item in the list view would not actually be rescanned
   -- Had to clear the UpperPrevSN and PrevShowName vars so that the second time the button was pressed it would compare the correct names

>>>>Paradox Changelog<<<<

1.42.1 (2-5-2010)
   - Added new checkbox and supporting code that allows you to save the xmls downloaded in data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml to make future renames quicker.
   -- Added check that determines if you are connected to the internet and thetvdb.com is reachable
   - Added better handling for previous showname
   -- Clean up some code that caused shows to use the previous renamed file settings wrongly
   - Renamed GetShowName(FileName) to GetName(FileName) and added new GetShowName(FileName, FileDir) to implement the following feature without requiring big changes to existing scripts.
   -- Added GetNameFromDir(FileName, Directory) to handle using the directory name as the showname, in case you are renaming files named 01x01 - title.avi the directory must, of course, be named as the show name.
   - Added debug message box if the episode info xml can't be read for some reason.
   -- Fixed Bug where first file would be properly renamed "Castle [2009] - 01x01 - Title" and the rest would be improperly renamed Castle - 01x02 - Title"
   - I am planning to add more features including using the API properly to download All the Episode information for a show and cache it locally/update on a regular basis.
   -- Also Planning to add a feature that will recognize when you are missing episodes from a season and either create a .txt file with the show name, season, episode, and title in the directory, or, keep track of it in a log file of some sort.
1.42.2 (2-5-2010)
   - Added support for choosing if you want to use spaces, periods or underscores as separators
   -- Added A checkbox that allows you to clear the list after renaming, so you don't have to click the button :)
   - Moved some things on the interface around a bit to accommodate previous features.
   -- Fixed an error where the list of files in the directory were being displayed after renaming instead of the previous items, renamed.
   - Added an option that allows you to click on rename and have it do the TVDB lookup and rename all in one (Be careful with this option)
   -- Fix up a few other small bugs
1.42.3 (2-5-2010)
   - Remove Some debug code I forgot to remove before.
   -- Save original title (The part between the 01x01 and the extension), if any to be added back later if no official title is found.
   - Implement better way of refreshing the display after a rename.
1.42.4 (2-5-2010)
   - Fix Typo that would keep files with no . in them at all from being renamed properly. (Exists in original 1.42 script as well)
1.42.5 (2-6-2010)
   - Fix renaming files with no Season and Episode Number (use original filename)
1.42.5a (2-8-2010)
  - Re-added code to read/keep the info of the release version... [by Pacheco]
1.42.6 (2-9-2010)
  - Fixed a typo where a closing curly brace } was in the wrong place.
*/


Paradox
  • Members
  • 24 posts
  • Last active: Mar 05 2013 04:15 PM
  • Joined: 05 Feb 2010
OK, one more, this fixes the handling of Episodes without the release info...

1.42.7
Edit, I went ahead and made the change mentioned in the next post here, so, if you already copy+pasted this, do it again.

; SceneRenamer by strictlyfocused02
; Modified By Paradox iamparadox at netscape.net
; AutoHotkey Version: 1.0.48+
;
; Script Function:
;   Extracts season and episode info from the filename
;   and uses that to retreive info from TheTVDB.com to rename
;   the file to a user-specified format

;   Special thanks to TheTVDB.com
;   This project would not have been possible without their top-notch API

#Include xpath.ahk

#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%

WindowTitle = SceneRenamer v1.42.7 Modified By Paradox

Gui, Add, Button, Default gButtonAddFolder, &Add folder
Gui, Add, Button, x+20 gButtonAddFile, &Add file
Gui, Add, Button, x+20 gButtonClear, &Clear list
Gui, Add, Text, x+150 w50, Status:
Gui, Add, Edit, +ReadOnly x+0 w400, Waiting for user to add file(s)...

Gui, Add, ListView, xm r26 w800 -ReadOnly vMyListView, Name|Preview|Directory|Extension|TVDB Series ID No

Gui, Add, DropDownList, w300 AltSubmit vNameScheme, (Show Title) - (SS)x(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title)|(SS)x(EE) - Episode Title||S(SS)E(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title) - (Release)
Gui, Add, DropDownList, x+10 w180 AltSubmit vSeparator, Use spaces as separators|Use.periods.as.separators||Use_underscores_as_separators
Gui, Add, Button, x+100 gButtonGetTVDBInfo, &Get TVDB Info
Gui, Add, Button, x+10 gButtonRename, &Rename files

Gui, Add, Checkbox, xm Checked vZero, Pad with zero?
Gui, Add, Checkbox, x+10 Checked vCache, Cache Episode Info?
Gui, Add, Checkbox, x+10 vRename, Allow Direct Rename?
Gui, Add, Checkbox, x+10 Checked vClear, Clear List after Rename?

Menu, MyContextMenu, Add, Open, ContextOpenFile
Menu, MyContextMenu, Add, Properties, ContextProperties
Menu, MyContextMenu, Add, Remove item, ContextRemoveItem

Gui, Show,, %WindowTitle%
Return

GuiClose:
   ExitApp

GuiContextMenu:
   If A_GuiControl <> MyListView
       Return
   Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
   Return

ContextOpenFile:
ContextProperties:
   FocusedRowNumber := LV_GetNext(0, "F")
   If not FocusedRowNumber
      return
   LV_GetText(FileName, FocusedRowNumber, 1)
   LV_GetText(FileDir, FocusedRowNumber, 3)
   IfInString A_ThisMenuItem, Open
      Run %FileDir%\%FileName%,, UseErrorLevel
   Else
      Run Properties "%FileDir%\%FileName%",, UseErrorLevel
   If ErrorLevel
      MsgBox Could not perform requested action on "%FileDir%\%FileName%".
   return

ContextRemoveItem:
   RowNumber = 0
   Loop
   {
      RowNumber := LV_GetNext(RowNumber - 1)
      If not RowNumber
           Break
      LV_Delete(RowNumber)
   }
   Return

ButtonAddFolder:
   Gui +OwnDialogs
   FileSelectFolder, TargetFolder,, 3, Select a folder:
   If not TargetFolder  ; The user pressed cancel
       Return
   StringRight, LastChar, TargetFolder, 1
   If LastChar = \
      StringTrimRight, TargetFolder, TargetFolder, 1
   GuiControl, -Redraw, MyListView ; Improves performance by disabling redrawing
   Loop %TargetFolder%\*.*
   {
      FileName := A_LoopFileFullPath
      SplitPath, FileName,,, FileExt  ; Get the file extension
      If FileExt in avi,mkv,mp4,srt
         LV_Add(Row, A_LoopFileName, "", A_LoopFileDir, FileExt)
   }
   GuiControl, +Redraw, MyListView  ; Turn redrawing back on
   LV_ModifyCol()  ; Resize each column to fit
   LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
   LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
   LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   ControlSetText, Edit1, Added folder %TargetFolder%, %WindowTitle%
   Return

ButtonAddFile:
   Gui +OwnDialogs
   FileSelectFile, TargetFile
   If not TargetFile
      Return
   SplitPath, TargetFile,,, FileExt  ; Get the file extension
   If FileExt in avi,mkv,mp4,srt
   {
      SplitPath, TargetFile, FileName, TargetFolder
      LV_Add(Row, FileName, "", TargetFolder, FileExt)
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      ControlSetText, Edit1, Added file %TargetFile%..., %WindowTitle%
   }
   Return

ButtonClear:
   ClearList()
   Return

ClearList()
{
   TargetFolder =
   TargetFile =
   LV_Delete()
   ControlSetText, Edit1, Cleared Listview, %WindowTitle%
   Return
}

InternetCheckConnection(Url="",FIFC=1) {
   Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0)
}

ButtonGetTVDBInfo:
   GetTVDBInfo()
   Return

GetTVDBInfo()
{
   LV_GetText(LVFileName, 1, 1)
   If not LVFileName
   {
      Gui +OwnDialogs
      MsgBox, 0,Fatal Error!, You must select a file or folder before connecting to TheTVDB.com!
      Return
   }
   LV_GetText(LVFileDir, 1, 3)  ; Get the file directory
   LV_GetText(LVFileExt, 1, 4)  ; Get the file extension

   URL := "http://www.thetvdb.com"

   If not InternetCheckConnection(URL)
   {
      Msgbox, 48, [%URL%]  , Connection Failed!
      Return
   }

   RowNumber = 1
   ShowInfo := GetTVDBShows(GetShowName(LVFileName, LVFileDir))
   StringSplit, ShowArray, ShowInfo, `,
   ShowName = %ShowArray1%
   SeriesIDNo = %ShowArray2%
   Loop
   {
      Gui +OwnDialogs
      If A_Index > 1
      {
         LV_GetText(LVPrevFN, RowNumber - 1, 1)  ; Get the preceeding rows filename
         LV_GetText(LVPrevFD, RowNumber - 1, 3)  ; Get the preceeding rows file directory
         PrevShowName := GetShowName(LVPrevFN, LVPrevFD)
         StringUpper, UpperPrevSN, PrevShowName

         NewShowName := GetShowName(LVFileName, LVFileDir)
         StringUpper, UpperCurrSN, NewShowName
         If UpperCurrSN <> %UpperPrevSN%
         {
            ShowInfo := GetTVDBShows(NewShowName)
            StringSplit, ShowArray, ShowInfo, `,
            ShowName = %ShowArray1%
            SeriesIDNo = %ShowArray2%
         }
      }
      UpperPrevSN =
      SeasonNumber := GetSeasonNumber(LVFileName)
      EpisodeNumber := GetEpisodeNumber(LVFileName)
      OriginalTitle := GetTitle(LVFileName)
      ReleaseInfo := GetRelease(LVFileName)
      ControlSetText, Edit1, Processing "%ShowName%" Season "%SeasonNumber%" Episode "%EpisodeNumber%", %WindowTitle%
      If SeriesIDNo
      {
         IfInString, ShowName, %A_Space%
            StringReplace, SafeShowName, ShowName, %A_Space%, _, All
         Else
            SafeShowName = %ShowName%
         EpisodeInfo := GetTVDBEpisodes(SeriesIDNo, SeasonNumber, EpisodeNumber, SafeShowName)
         StringSplit, OutputArray, EpisodeInfo, `,
         SeasonNumber = %OutputArray1%
         EpisodeNumber = %OutputArray2%
         EpisodeTitle = %OutputArray3%
         If EpisodeTitle
         {
            StringReplace, EpisodeTitle, EpisodeTitle, \, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, /, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, :, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, *, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, ?,, All
            StringReplace, EpisodeTitle, EpisodeTitle, ", ', All
            StringReplace, EpisodeTitle, EpisodeTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, |, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, &`#44;, `,, All
            StringReplace, EpisodeTitle, EpisodeTitle, &, &, All
         }
         If OriginalTitle
         {
            StringReplace, OriginalTitle, OriginalTitle, \, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, /, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, :, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, *, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, ?,, All
            StringReplace, OriginalTitle, OriginalTitle, ", ', All
            StringReplace, OriginalTitle, OriginalTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, |, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, &`#44;, `,, All
            StringReplace, OriginalTitle, OriginalTitle, &, &, All
         }
         GuiControlGet, PadZero,, Zero
         If PadZero = 1
         {
            SeasonNumber := PadZero(SeasonNumber)
            EpisodeNumber := PadZero(EpisodeNumber)
         }
         GuiControlGet, NamingScheme,, NameScheme
         If NamingScheme = 1
         {
            If EpisodeTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber%
         }
         Else if NamingScheme = 2
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 3
         {
            If EpisodeTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %SeasonNumber%x%EpisodeNumber%
         }
         Else If NamingScheme = 4
         {
            If EpisodeTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 5
         {
            If StrLen(ReleaseInfo) > 0
            {
               If EpisodeTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle% - %ReleaseInfo%
               Else If OriginalTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle% - %ReleaseInfo%
               Else
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %ReleaseInfo%
            }
            Else
            {
               If EpisodeTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
               Else If OriginalTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
               Else
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
            }
         }
      }
      Else
      {
         StringReplace, LVFN, LVFileName, \, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, /, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, :, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, *, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, ?,, All
         StringReplace, LVFN, LVFN, ", ', All
         StringReplace, LVFN, LVFN, <, %A_Space%less than%A_Space%, All
         StringReplace, LVFN, LVFN, >, %A_Space%greater than%A_Space%, All
         StringReplace, LVFN, LVFN, |, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, &`#44;, `,, All
         StringReplace, LVFN, LVFN, &, &, All
         StringTrimRight, LVFN, LVFN, 4
         Loop
         {
            StringRight, LastChar, LVFN, 1
            If (LastChar = "-" or LastChar = " ")
               StringTrimRight, LVFN, LVFN, 1
            Else
               Break
         }
         NewFileName = %LVFN%
      }
      GuiControlGet, Sep,, Separator
      If Sep = 2
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, ., All
      }
      Else If Sep = 3
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, _, All
      }
      NewFileName = %NewFileName%.%LVFileExt%
      LV_Modify(RowNumber, Col1, LVFileName, NewFileName, LVFileDir, LVFileExt, SeriesIDNo)
      LV_ModifyCol()
      LV_ModifyCol(4, 60)
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      LVFileName =
      LVPrevFN =
      ShowArray1 =
      OutputArray1 =
      OutputArray2 =
      OutputArray3 =
      SeasonNumber =
      EpisodeNumber =
      EpisodeTitle =
      OriginalTitle =
      RowNumber := RowNumber + 1
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the filename
      If not LVFileName  ; Break the loop at the end of the list
         Break
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file directory
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
   }
   ControlSetText, Edit1, Finished querying TheTVDB.com. Ready to rename..., %WindowTitle%
   Return
}

ButtonRename:
{
   LV_GetText(LVNewFileName, 1, 2)
   If not LVNewFileName
   {
      GuiControlGet, DirectRename,, Rename
      If DirectRename = 1
      {
         GetTVDBInfo()
      }
      Else
      {
         Gui +OwnDialogs
         MsgBox, 0,Fatal Error!, You must scan your files against TheTVDB.com before you rename them!
         Return
      }
   }
   Loop
   {
      RowNumber = %A_Index%
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the original filename
      LV_GetText(LVNewFileName, RowNumber, 2)  ; Get the new filename
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file dir
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
      If not LVNewFileName  ; Break the loop at the end of the list
         Break
      ControlSetText, Edit1, Renaming "%LVFileName%" to "%LVNewFileName%", %WindowTitle%
      FileMove, %LVFileDir%\%LVFileName%, %LVFileDir%\%LVNewFileName%  ; Finally, rename it
      LV_Modify(RowNumber, Col1, LVNewFileName, "", LVFileDir, LVFileExt, SeriesIDNo)
   }
   RenameCount := RowNumber - 1
   ControlSetText, Edit1, Finished renaming files! Refreshing file list., %WindowTitle%
   GuiControlGet, ClearIt,, Clear
   If ClearIt = 1
   {
      ClearList()
   }
   Else
   {
      GuiControl, +Redraw, MyListView  ; Turn redrawing back on
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   }
   ControlSetText, Edit1, Finished Successfully!, %WindowTitle%
   Return
}

;Provide a filename (greek.s01e01.original.title.avi) and this should return the original title (in case a title isn't found)
GetTitle(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Loop
   {
      StringRight, LastChar, ShowTitle, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Loop
   {
      StringLeft, FirstChar, ShowTitle, 1
      If (FirstChar = "-" or FirstChar = " ")
         StringTrimLeft, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Return ShowTitle
}

;Provide a filename (s01e01.avi or greek.s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetShowName(FileName, FileDir)
{
   TempName := GetName(FileName)
   If not TempName
      TempName := GetNameFromDir(FileName, FileDir)
   Return TempName
}
;Provide a filename (greek.s01e01.avi) and this should return the show name
GetName(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Loop
   {
      StringRight, LastChar, ShowName, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowName, ShowName, 1
      Else
         Break
   }
   If SeasonAndEpisodePos > -1
      Return ShowName
   Else
   {
      Return FileName
   }
}

;Provide a filename (s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetNameFromDir(FileName, FileDir)
{
   Directory = %FileDir% - %FileName%
   SplitPath , Directory, NewFileName, dir, ext, name
   Return GetName(NewFileName)
}

;Provide a filename (greek.s01e01.avi) and this should return the season number
GetSeasonNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }

   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringTrimRight, SeasonNumber, SeasonAndEpisode, 2
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Return SeasonNumber
}

;Provide a filename (greek.s01e01.avi) and this should return the episode number
GetEpisodeNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?.?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringRight, EpisodeNumber, SeasonAndEpisode, 2
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Return EpisodeNumber
}

;============================== RELEASE ==========================
GetRelease(FileName)
{
   ; Stores the source, codec and group to use latter
   RELEASE =

   ;~ GET IF IS REPACK ou PROPER
   StringGetPos, REPACK, FileName, repack, R, 4
   If (REPACK <> -1)
      RELEASE = %RELEASE%REPACK.

   StringGetPos, PROPER, FileName, proper, R, 4
   If (PROPER <> -1)
      RELEASE = %RELEASE%PROPER.
   
   ;~ GET SOURCE!
   StringGetPos, Source_720p, FileName, 720p, R, 4
   If (Source_720p <> -1)
      RELEASE = %RELEASE%720p.

   StringGetPos, Source_HDTV, FileName, HDTV, R, 4
   If (Source_HDTV >= 7)
      RELEASE = %RELEASE%HDTV.

   StringGetPos, Source_DVDRip, FileName, dvdrip, R, 4
   If (Source_DVDRip >= 7)
      RELEASE = %RELEASE%DVDRip.
   
   ;~ GET CODEC!
   StringGetPos, CODEC_XVID, FileName, xvid, R, 4
   If (CODEC_XVID <> -1)
      RELEASE = %RELEASE%XviD-

   StringGetPos, CODEC_X264, FileName, x264, R, 4
   If (CODEC_X264 <> -1)
      RELEASE = %RELEASE%x264-

   StringGetPos, CODEC_DIVX, FileName, divx, R, 4
   If (CODEC_DIVX <> -1)
      RELEASE = %RELEASE%DivX-
 
   ;nasty code but that works.... :S
   TAGSOFFSET := CODEC_XVID+CODEC_DIVX+CODEC_X264
   If TAGSOFFSET > 0
   {
      OFFSET := 1+TAGSOFFSET+5
      StringRight, GROUP, FileName, StrLen(FileName)-OFFSET
      StringTrimRight, GROUP, GROUP, 4
   }
   RELEASE = %RELEASE%%GROUP%
 
   ;MsgBox, DVDRip = %Source_DVDRip%`nXviD = %CODEC_XVID%`nDivX = %CODEC_DIVX%`nOFFSET = %OFFSET%`nGROUP = %GROUP%`nSUFFIX = %SUFFIX%
 
   Return RELEASE
}
;=================================================================


;Provide a show name (greek) and it will return the TVDB data for the show name (Greek) and series ID (80301) as a CSV (Greek,80301)
GetTVDBShows(ShowName)
{
   Global SeriesName, SeriesIDNum
   IfExist, Series_ID.xml
      FileDelete, Series_ID.xml
   UrlDownloadToFile, http://www.thetvdb.com/api/GetSeries.php?seriesname="%ShowName%", Series_ID.xml
   XPath_Load(xml, "Series_ID.xml")
   FileDelete, Series_ID.xml
   SeriesNameNodeCount := XPath(xml, "/Data/Series/SeriesName/Count()")
   If SeriesNameNodeCount = 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
   }
   Else If SeriesNameNodeCount > 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
      SeriesOverview := XPath(xml, "/Data/Series/Overview/text()")
      StringSplit, OutputArraySID, SeriesIDNum, `,
      StringSplit, OutputArraySN, SeriesName, `,
      StringSplit, OutputArraySO, SeriesOverview, `,
      Gui, 2: Default
      Gui, 2: +ToolWindow +Owner1
      Gui, 2: Add, ListView, xm r15 w600, Show name|Show description|TVDB series ID no.
      Gui, 2: Add, Text, y+7
      Gui, 2: Add, Button, x+475, Select
      Gui, 2: Add, Button, x+15, Cancel
      Loop, %SeriesNameNodeCount%
      {
         SeriesIDNum := OutputArraySID%A_Index%
         SeriesName := OutputArraySN%A_Index%
         SeriesOverview := OutputArraySO%A_Index%
         IfInString, SeriesOverview, ,`;
            StringReplace, SeriesOverview, SeriesOverview, ,`;, `,, All
         If not SeriesOverview
            SeriesOverview = (No show description provided)
         LV_Add(Row, SeriesName, SeriesOverview, SeriesIDNum)
         LV_ModifyCol()
         LV_ModifyCol(3, 110)
         SeriesIDNum =
         SeriesName =
         SeriesOverview =
      }
      Gui, 2: Show,,Which Show?
      Gui, 1: Default
      WinWaitClose, Which Show?
   }
   If not SeriesName
      SeriesName = %ShowName%
   J = %SeriesName%,%SeriesIDNum%
   ShowName =
   SeriesIDNum =
   SeriesName =
   SeriesOverview =
   Return J
}

   ;Labels required for Gui 2
   2GuiEscape:
   2GuiClose:
   2ButtonCancel:
   {
      Gui, 2: Destroy
      Return
   }
   2ButtonSelect:
   {
      FocusedRowNumber := LV_GetNext(0, "F")
      If not FocusedRowNumber
      {
         MsgBox, 0, Warning!, You must select a show or press cancel!
         Return
      }
      LV_GetText(SeriesName, FocusedRowNumber, 1)
      LV_GetText(SeriesIDNum, FocusedRowNumber, 3)
      FocusedRowNumber =
      Gui, 2: Destroy
      Return
   }

;Provide the "TVDB series ID", "season number" and "episode number" and this will return the TVDB season number, episode number, and episode title as a CSV (3,1,The Day After)
GetTVDBEpisodes(SeriesIDNum, SeasonNum, EpisodeNum, ShowName)
{
   GuiControlGet, CacheInfo,, Cache
   FileName = data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml
   If CacheInfo = 1
   {
      IfNotExist, FileName
         UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, %FileName%
      If not XPath_Load(xml, FileName)
         Msgbox, 48, [XPath]  , Load of %FileName% Failed!
   }
   Else
   {
      IfExist, Episode_Info.xml
         FileDelete, Episode_Info.xml
      UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, Episode_Info.xml
      If not XPath_Load(xml, "Episode_Info.xml")
         Msgbox, 48, [XPath]  , Load of "Episode_Info.xml" Failed!
      FileDelete, Episode_Info.xml
   }
   SeasonNumber := XPath(xml, "/Data/Episode/SeasonNumber/text()")
   EpisodeNumber := XPath(xml, "/Data/Episode/EpisodeNumber/text()")
   EpisodeTitle := XPath(xml, "/Data/Episode/EpisodeName/text()")
   If not EpisodeTitle
   {
      IfExist, %FileName%
         FileDelete, %FileName%
   }
   If not SeasonNumber
      SeasonNumber = %SeasonNum%
   If not EpisodeNumber
      EpisodeNumber = %EpisodeNum%
   K = %SeasonNumber%,%EpisodeNumber%,%EpisodeTitle%
   SeasonNumber =
   EpisodeNumber =
   EpisodeTitle =
   Return K
}

;Pads a variable with a preceeding zero
PadZero(VarToPad)
{
   VarToPadLen := StrLen(VarToPad)
   If VarToPadLen = 1
      VarToPad = 0%VarToPad%
   VarToPadLen =
   Return VarToPad
}

/*
>>>>CHANGELOG<<<<

1.0 (10-5-2009)
   - Original script
1.1 (10-6-2009)
   - Fixed a bug with adding a single file where the file would not be renamed
   -- Forgot to define the TargetFile's TargetFolder when updating the GUI
1.2 (10-13-2009)
   - Fixed another bug with adding a single file where the file would not be renamed
   - Fixed a bug with parsing the series overview where the non-comma delimiter (,) would be displayed
   -- Titans XPath replaces commas with "," (no quotes)
1.30 (11-8-2009)
   - Added a new method of selecting a show name when multiple results are returned
   -- Secondary ListView gui is displayed allowing the user to select the correct show
   - Script now compares the current filename against the previous filename (filenames converted to UPPERCASE)
   -- Drasitically reduces the number of requests against the TVDB and speeds everything up when renaming multiple shows all with the same name
   - Added a check and replace system for invalid filename characters
   -- StringReplace these chars \ / : * ? "" < > | with descriptive replacements
   - Fixed a bug in the GetEpisodeNumber() function
   -- A single digit season number would sometimes cause too few of the episode numbers to be returned when SeasonAndEpisode contained (e|x)
   - Tweaked GetSeasonNumber() and GetEpisodeNumber() to more reliably return correct data
   -- StringGetPos and String(Left|Right) were swapped for StringSplit to more predictably split SeasonAndEpisode regardless of (e|x) char location
   - Added a check to make sure files have a new filename in the preview column before renaming
   - Added a static window title which may be needed for various features in the future as well as making things look nicer :P
1.40 (11-15-2009)
   - Added status bar
   - Fixed a bug where a file would not have a preceeding zero added if no info was found on TheTVDB by adding PadZero() function
   -- Preceeding zero was only added in GetTVDBEpisodes() which means that if SeriesIDNo was blank (i.e. no info pulled from TheTVDB) the Season and Episode number would not get padded

1.40a (12-11-2009) by Pacheco
   - Added an option to preserve the information of [Source.Codec-Group] if present

1.41 (12-10-2009)
   - Fixed a serious bug where no results would be returned for ShowName inside the GetTVDBShows function would return the first result infinitely
   -- Had to clear the SeriesName var at the end of the function so that on the next loop iteration the correct var contents would exist
1.42 (12-11-2009)
   - Fixed a minor bug where the second time the GetTVDBInfo button was pressed, the first item in the list view would not actually be rescanned
   -- Had to clear the UpperPrevSN and PrevShowName vars so that the second time the button was pressed it would compare the correct names

>>>>Paradox Changelog<<<<

1.42.1 (2-5-2010)
   - Added new checkbox and supporting code that allows you to save the xmls downloaded in data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml to make future renames quicker.
   -- Added check that determines if you are connected to the internet and thetvdb.com is reachable
   - Added better handling for previous showname
   -- Clean up some code that caused shows to use the previous renamed file settings wrongly
   - Renamed GetShowName(FileName) to GetName(FileName) and added new GetShowName(FileName, FileDir) to implement the following feature without requiring big changes to existing scripts.
   -- Added GetNameFromDir(FileName, Directory) to handle using the directory name as the showname, in case you are renaming files named 01x01 - title.avi the directory must, of course, be named as the show name.
   - Added debug message box if the episode info xml can't be read for some reason.
   -- Fixed Bug where first file would be properly renamed "Castle [2009] - 01x01 - Title" and the rest would be improperly renamed Castle - 01x02 - Title"
   - I am planning to add more features including using the API properly to download All the Episode information for a show and cache it locally/update on a regular basis.
   -- Also Planning to add a feature that will recognize when you are missing episodes from a season and either create a .txt file with the show name, season, episode, and title in the directory, or, keep track of it in a log file of some sort.
1.42.2 (2-5-2010)
   - Added support for choosing if you want to use spaces, periods or underscores as separators
   -- Added A checkbox that allows you to clear the list after renaming, so you don't have to click the button :)
   - Moved some things on the interface around a bit to accommodate previous features.
   -- Fixed an error where the list of files in the directory were being displayed after renaming instead of the previous items, renamed.
   - Added an option that allows you to click on rename and have it do the TVDB lookup and rename all in one (Be careful with this option)
   -- Fix up a few other small bugs
1.42.3 (2-5-2010)
   - Remove Some debug code I forgot to remove before.
   -- Save original title (The part between the 01x01 and the extension), if any to be added back later if no official title is found.
   - Implement better way of refreshing the display after a rename.
1.42.4 (2-5-2010)
   - Fix Typo that would keep files with no . in them at all from being renamed properly. (Exists in original 1.42 script as well)
1.42.5 (2-6-2010)
   - Fix renaming files with no Season and Episode Number (use original filename)
1.42.5a (2-8-2010)
  - Re-added code to read/keep the info of the release version... [by Pacheco]
1.42.6 (2-9-2010)
  - Fixed a typo where a closing curly brace } was in the wrong place.
1.42.7 (2-9-2010)
  - Fixed handling of files without release info such as group and format.
*/


Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
Hi paradox....
In fact that's true... i forgot to add that possibility... :S (Thanks)

Meanwhile i found another little bug in mine+yours version of my code:

the line:
;nasty code but that works.... :S
   TAGSOFFSET := CODEC_XVID+CODEC_DIVX
   If TAGSOFFSET > 0
must be

;nasty code but that works.... :S
   TAGSOFFSET := CODEC_XVID+CODEC_DIVX[color=red]+CODEC_X264[/color]
   If TAGSOFFSET > 0

or when using a x264 file, the code will miss to get the correct release info...

Thanks for your corrections :)
(re-edit your last post, and add the modifications)

Paradox
  • Members
  • 24 posts
  • Last active: Mar 05 2013 04:15 PM
  • Joined: 05 Feb 2010
You should also hunt around and collect all the possible tags, because, I already know you are missing WS or WS-PDTV or, even just PDTV and probably many more.....

AC3 MP3.... there should be some sort of scene reference someplace.

Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
@Paradox...

Yeah, i know that are some "TAGS" missing...
I intend to review all my code to use RegEx (but i'm still learning it), to make the release detection able to handle file names with {spaces}{dots}{underscores}, something like:

[{space}{dot}{underscore}]{RELEASE_TAGS}[{space}{dot}{underscore}]

for example:
CODEC_HDTV = [{space}{dot}{underscore}]HDTV[{space}{dot}{underscore}]

When i have time, i'll learn how to do it and will make it :)

joazito
  • Guests
  • Last active:
  • Joined: --
Wow this thread has really taken off since I last saw it. I stumbled on it a few weeks ago when I was trying to find a renamer that preserved scene info ("How.I.Met.Your.Mother.S05E10.The.Window.HDTV.XviD-FQM") and bookmarked this thread as it seemed the closest I could find.

I would be very interesting in seeing your developments, Paradox.

Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
@joazito

Was not Paradox that made that part, but ok... :?

joazito
  • Guests
  • Last active:
  • Joined: --
Hehe sorry Pacheco I kinda just skimmed through the posts and noticed Paradox's comment about the lack of interest and decided to post something.

In retrospect I should have acknowledged your efforts. Specially because you're my countryman :).

If you ever do need some help with those regexes or something, I'll be more than happy to try and help out.

Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
As promised, I've learning a bit of ReGex, and I've changed my part of the code (the RELEASE info), and added some more tags. Now it detects and renames episodes that can contain any of this tags:

WS
EXTENDED
PREAIR
PROPER
REPACK
REAL
INTERNAL
720p
1080i
1080p
HDTV
PDTV
HR
XviD
x264
DivX


Any more "relevant" tag, let me know....

; SceneRenamer by strictlyfocused02
; Modified By Paradox iamparadox at netscape.net
; AutoHotkey Version: 1.0.48+
;
; Script Function:
;   Extracts season and episode info from the filename
;   and uses that to retreive info from TheTVDB.com to rename
;   the file to a user-specified format

;   Special thanks to TheTVDB.com
;   This project would not have been possible without their top-notch API

#Include xpath.ahk

#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%

WindowTitle = SceneRenamer v1.42.7 Modified By Paradox

Gui, Add, Button, Default gButtonAddFolder, &Add folder
Gui, Add, Button, x+20 gButtonAddFile, &Add file
Gui, Add, Button, x+20 gButtonClear, &Clear list
Gui, Add, Text, x+150 w50, Status:
Gui, Add, Edit, +ReadOnly x+0 w400, Waiting for user to add file(s)...

Gui, Add, ListView, xm r26 w800 -ReadOnly vMyListView, Name|Preview|Directory|Extension|TVDB Series ID No

Gui, Add, DropDownList, w300 AltSubmit vNameScheme, (Show Title) - (SS)x(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title)|(SS)x(EE) - Episode Title|S(SS)E(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title) - (Release)||
Gui, Add, DropDownList, x+10 w180 AltSubmit vSeparator, Use spaces as separators||Use.periods.as.separators|Use_underscores_as_separators
Gui, Add, Button, x+100 gButtonGetTVDBInfo, &Get TVDB Info
Gui, Add, Button, x+10 gButtonRename, &Rename files

Gui, Add, Checkbox, xm Checked vZero, Pad with zero?
Gui, Add, Checkbox, x+10 vCache, Cache Episode Info?
Gui, Add, Checkbox, x+10 vRename, Allow Direct Rename?
Gui, Add, Checkbox, x+10 Checked vClear, Clear List after Rename?

Menu, MyContextMenu, Add, Open, ContextOpenFile
Menu, MyContextMenu, Add, Properties, ContextProperties
Menu, MyContextMenu, Add, Remove item, ContextRemoveItem

Gui, Show,, %WindowTitle%
Return

GuiClose:
   ExitApp

GuiContextMenu:
   If A_GuiControl <> MyListView
       Return
   Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
   Return

ContextOpenFile:
ContextProperties:
   FocusedRowNumber := LV_GetNext(0, "F")
   If not FocusedRowNumber
      return
   LV_GetText(FileName, FocusedRowNumber, 1)
   LV_GetText(FileDir, FocusedRowNumber, 3)
   IfInString A_ThisMenuItem, Open
      Run %FileDir%\%FileName%,, UseErrorLevel
   Else
      Run Properties "%FileDir%\%FileName%",, UseErrorLevel
   If ErrorLevel
      MsgBox Could not perform requested action on "%FileDir%\%FileName%".
   return

ContextRemoveItem:
   RowNumber = 0
   Loop
   {
      RowNumber := LV_GetNext(RowNumber - 1)
      If not RowNumber
           Break
      LV_Delete(RowNumber)
   }
   Return

ButtonAddFolder:
   Gui +OwnDialogs
   FileSelectFolder, TargetFolder,, 3, Select a folder:
   If not TargetFolder  ; The user pressed cancel
       Return
   StringRight, LastChar, TargetFolder, 1
   If LastChar = \
      StringTrimRight, TargetFolder, TargetFolder, 1
   GuiControl, -Redraw, MyListView ; Improves performance by disabling redrawing
   Loop %TargetFolder%\*.*
   {
      FileName := A_LoopFileFullPath
      SplitPath, FileName,,, FileExt  ; Get the file extension
      If FileExt in avi,mkv,mp4,srt
         LV_Add(Row, A_LoopFileName, "", A_LoopFileDir, FileExt)
   }
   GuiControl, +Redraw, MyListView  ; Turn redrawing back on
   LV_ModifyCol()  ; Resize each column to fit
   LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
   LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
   LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   ControlSetText, Edit1, Added folder %TargetFolder%, %WindowTitle%
   Return

ButtonAddFile:
   Gui +OwnDialogs
   FileSelectFile, TargetFile
   If not TargetFile
      Return
   SplitPath, TargetFile,,, FileExt  ; Get the file extension
   If FileExt in avi,mkv,mp4,srt
   {
      SplitPath, TargetFile, FileName, TargetFolder
      LV_Add(Row, FileName, "", TargetFolder, FileExt)
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      ControlSetText, Edit1, Added file %TargetFile%..., %WindowTitle%
   }
   Return

ButtonClear:
   ClearList()
   Return

ClearList()
{
   TargetFolder =
   TargetFile =
   LV_Delete()
   ControlSetText, Edit1, Cleared Listview, %WindowTitle%
   Return
}

InternetCheckConnection(Url="",FIFC=1) {
   Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0)
}

ButtonGetTVDBInfo:
   GetTVDBInfo()
   Return

GetTVDBInfo()
{
   LV_GetText(LVFileName, 1, 1)
   If not LVFileName
   {
      Gui +OwnDialogs
      MsgBox, 0,Fatal Error!, You must select a file or folder before connecting to TheTVDB.com!
      Return
   }
   LV_GetText(LVFileDir, 1, 3)  ; Get the file directory
   LV_GetText(LVFileExt, 1, 4)  ; Get the file extension

   URL := "http://www.thetvdb.com"

   If not InternetCheckConnection(URL)
   {
      Msgbox, 48, [%URL%]  , Connection Failed!
      Return
   }

   RowNumber = 1
   ShowInfo := GetTVDBShows(GetShowName(LVFileName, LVFileDir))
   StringSplit, ShowArray, ShowInfo, `,
   ShowName = %ShowArray1%
   SeriesIDNo = %ShowArray2%
   Loop
   {
      Gui +OwnDialogs
      If A_Index > 1
      {
         LV_GetText(LVPrevFN, RowNumber - 1, 1)  ; Get the preceeding rows filename
         LV_GetText(LVPrevFD, RowNumber - 1, 3)  ; Get the preceeding rows file directory
         PrevShowName := GetShowName(LVPrevFN, LVPrevFD)
         StringUpper, UpperPrevSN, PrevShowName

         NewShowName := GetShowName(LVFileName, LVFileDir)
         StringUpper, UpperCurrSN, NewShowName
         If UpperCurrSN <> %UpperPrevSN%
         {
            ShowInfo := GetTVDBShows(NewShowName)
            StringSplit, ShowArray, ShowInfo, `,
            ShowName = %ShowArray1%
            SeriesIDNo = %ShowArray2%
         }
      }
      UpperPrevSN =
      SeasonNumber := GetSeasonNumber(LVFileName)
      EpisodeNumber := GetEpisodeNumber(LVFileName)
      OriginalTitle := GetTitle(LVFileName)
      ReleaseInfo := GetRelease(LVFileName)
      ControlSetText, Edit1, Processing "%ShowName%" Season "%SeasonNumber%" Episode "%EpisodeNumber%", %WindowTitle%
      If SeriesIDNo
      {
         IfInString, ShowName, %A_Space%
            StringReplace, SafeShowName, ShowName, %A_Space%, _, All
         Else
            SafeShowName = %ShowName%
         EpisodeInfo := GetTVDBEpisodes(SeriesIDNo, SeasonNumber, EpisodeNumber, SafeShowName)
         StringSplit, OutputArray, EpisodeInfo, `,
         SeasonNumber = %OutputArray1%
         EpisodeNumber = %OutputArray2%
         EpisodeTitle = %OutputArray3%
         If EpisodeTitle
         {
            StringReplace, EpisodeTitle, EpisodeTitle, \, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, /, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, :, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, *, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, ?,, All
            StringReplace, EpisodeTitle, EpisodeTitle, ", ', All
            StringReplace, EpisodeTitle, EpisodeTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, |, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, &`#44;, `,, All
            StringReplace, EpisodeTitle, EpisodeTitle, &, &, All
         }
         If OriginalTitle
         {
            StringReplace, OriginalTitle, OriginalTitle, \, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, /, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, :, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, *, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, ?,, All
            StringReplace, OriginalTitle, OriginalTitle, ", ', All
            StringReplace, OriginalTitle, OriginalTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, |, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, &`#44;, `,, All
            StringReplace, OriginalTitle, OriginalTitle, &, &, All
         }
         GuiControlGet, PadZero,, Zero
         If PadZero = 1
         {
            SeasonNumber := PadZero(SeasonNumber)
            EpisodeNumber := PadZero(EpisodeNumber)
         }
         GuiControlGet, NamingScheme,, NameScheme
         If NamingScheme = 1
         {
            If EpisodeTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber%
         }
         Else if NamingScheme = 2
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 3
         {
            If EpisodeTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %SeasonNumber%x%EpisodeNumber%
         }
         Else If NamingScheme = 4
         {
            If EpisodeTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 5
         {
            If StrLen(ReleaseInfo) > 0
            {
               If EpisodeTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle% - %ReleaseInfo%
               Else If OriginalTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle% - %ReleaseInfo%
               Else
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %ReleaseInfo%
            }
            Else
            {
               If EpisodeTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
               Else If OriginalTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
               Else
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
            }
         }
      }
      Else
      {
         StringReplace, LVFN, LVFileName, \, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, /, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, :, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, *, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, ?,, All
         StringReplace, LVFN, LVFN, ", ', All
         StringReplace, LVFN, LVFN, <, %A_Space%less than%A_Space%, All
         StringReplace, LVFN, LVFN, >, %A_Space%greater than%A_Space%, All
         StringReplace, LVFN, LVFN, |, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, &`#44;, `,, All
         StringReplace, LVFN, LVFN, &, &, All
         StringTrimRight, LVFN, LVFN, 4
         Loop
         {
            StringRight, LastChar, LVFN, 1
            If (LastChar = "-" or LastChar = " ")
               StringTrimRight, LVFN, LVFN, 1
            Else
               Break
         }
         NewFileName = %LVFN%
      }
      GuiControlGet, Sep,, Separator
      If Sep = 2
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, ., All
      }
      Else If Sep = 3
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, _, All
      }
      NewFileName = %NewFileName%.%LVFileExt%
      LV_Modify(RowNumber, Col1, LVFileName, NewFileName, LVFileDir, LVFileExt, SeriesIDNo)
      LV_ModifyCol()
      LV_ModifyCol(4, 60)
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      LVFileName =
      LVPrevFN =
      ShowArray1 =
      OutputArray1 =
      OutputArray2 =
      OutputArray3 =
      SeasonNumber =
      EpisodeNumber =
      EpisodeTitle =
      OriginalTitle =
      RowNumber := RowNumber + 1
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the filename
      If not LVFileName  ; Break the loop at the end of the list
         Break
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file directory
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
   }
   ControlSetText, Edit1, Finished querying TheTVDB.com. Ready to rename..., %WindowTitle%
   Return
}

ButtonRename:
{
   LV_GetText(LVNewFileName, 1, 2)
   If not LVNewFileName
   {
      GuiControlGet, DirectRename,, Rename
      If DirectRename = 1
      {
         GetTVDBInfo()
      }
      Else
      {
         Gui +OwnDialogs
         MsgBox, 0,Fatal Error!, You must scan your files against TheTVDB.com before you rename them!
         Return
      }
   }
   Loop
   {
      RowNumber = %A_Index%
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the original filename
      LV_GetText(LVNewFileName, RowNumber, 2)  ; Get the new filename
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file dir
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
      If not LVNewFileName  ; Break the loop at the end of the list
         Break
      ControlSetText, Edit1, Renaming "%LVFileName%" to "%LVNewFileName%", %WindowTitle%
      FileMove, %LVFileDir%\%LVFileName%, %LVFileDir%\%LVNewFileName%  ; Finally, rename it
      LV_Modify(RowNumber, Col1, LVNewFileName, "", LVFileDir, LVFileExt, SeriesIDNo)
   }
   RenameCount := RowNumber - 1
   ControlSetText, Edit1, Finished renaming files! Refreshing file list., %WindowTitle%
   GuiControlGet, ClearIt,, Clear
   If ClearIt = 1
   {
      ClearList()
   }
   Else
   {
      GuiControl, +Redraw, MyListView  ; Turn redrawing back on
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   }
   ControlSetText, Edit1, Finished Successfully!, %WindowTitle%
   Return
}

;Provide a filename (greek.s01e01.original.title.avi) and this should return the original title (in case a title isn't found)
GetTitle(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Loop
   {
      StringRight, LastChar, ShowTitle, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Loop
   {
      StringLeft, FirstChar, ShowTitle, 1
      If (FirstChar = "-" or FirstChar = " ")
         StringTrimLeft, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Return ShowTitle
}

;Provide a filename (s01e01.avi or greek.s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetShowName(FileName, FileDir)
{
   TempName := GetName(FileName)
   If not TempName
      TempName := GetNameFromDir(FileName, FileDir)
   Return TempName
}
;Provide a filename (greek.s01e01.avi) and this should return the show name
GetName(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Loop
   {
      StringRight, LastChar, ShowName, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowName, ShowName, 1
      Else
         Break
   }
   If SeasonAndEpisodePos > -1
      Return ShowName
   Else
   {
      Return FileName
   }
}

;Provide a filename (s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetNameFromDir(FileName, FileDir)
{
   Directory = %FileDir% - %FileName%
   SplitPath , Directory, NewFileName, dir, ext, name
   Return GetName(NewFileName)
}

;Provide a filename (greek.s01e01.avi) and this should return the season number
GetSeasonNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }

   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringTrimRight, SeasonNumber, SeasonAndEpisode, 2
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Return SeasonNumber
}

;Provide a filename (greek.s01e01.avi) and this should return the episode number
GetEpisodeNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?.?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringRight, EpisodeNumber, SeasonAndEpisode, 2
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Return EpisodeNumber
}

;============================== RELEASE ==========================
GetRelease(FileName)
{
   ; Stores the source, codec and group to use latter
   RELEASE =

   ; FoundPos := RegExMatch( VAR, "i)[\._[:space:]]hdtv[\._[:space:]]")

   ;~ Check EXTRA tags
   WS := RegExMatch( Filename, "i)[\._[:space:]]ws[\._[:space:]]")
   If (WS >= 7)
      RELEASE = %RELEASE%WS.

   EXTENDED := RegExMatch( Filename, "i)[\._[:space:]]extended[\._[:space:]]")
   If (EXTENDED >= 7)
      RELEASE = %RELEASE%EXTENDED.

   INTERNAL := RegExMatch( Filename, "i)[\._[:space:]]internal[\._[:space:]]")
   If (INTERNAL >= 7)
      RELEASE = %RELEASE%INTERNAL
      
   PREAIR := RegExMatch( Filename, "i)[\._[:space:]]preair[\._[:space:]]")
   If (PREAIR >= 7)
      RELEASE = %RELEASE%PREAIR

   FINAL := RegExMatch( Filename, "i)[\._[:space:]]final[\._[:space:]]")
   If (FINAL >= 7)
      RELEASE = %RELEASE%FINAL.

   REPACK := RegExMatch( Filename, "i)[\._[:space:]]repack[\._[:space:]]")
   If (REPACK >= 7)
      RELEASE = %RELEASE%REPACK.

   PROPER := RegExMatch( Filename, "i)[\._[:space:]]proper[\._[:space:]]")
   If (PROPER >= 7)
      RELEASE = %RELEASE%PROPER.
   
   ;~ Get SOURCE!
   Source_720p := RegExMatch( Filename, "i)[\._[:space:]]720p[\._[:space:]]")
   If (Source_720p >= 7)
      RELEASE = %RELEASE%720p.

   Source_1080p := RegExMatch( Filename, "i)[\._[:space:]]1080p[\._[:space:]]")
   If (Source_1080p >= 7)
      RELEASE = %RELEASE%1080p.

   Source_1080i := RegExMatch( Filename, "i)[\._[:space:]]1080i[\._[:space:]]")
   If (Source_1080i >= 7)
      RELEASE = %RELEASE%1080i.

   Source_HDTV := RegExMatch( Filename, "i)[\._[:space:]]hdtv[\._[:space:]]")
   If (Source_HDTV >= 7)
      RELEASE = %RELEASE%HDTV.

   HR := RegExMatch( Filename, "i)[\._[:space:]]hr[\._[:space:]]")
   If (HR >= 7)
      RELEASE = %RELEASE%HR.

   Source_PDTV := RegExMatch( Filename, "i)[\._[:space:]]pdtv[\._[:space:]]")
   If (Source_PDTV >= 7)
      RELEASE = %RELEASE%PDTV.

   Source_DVDRip := RegExMatch( Filename, "i)[\._[:space:]]dvdrip[\._[:space:]]")
   If (Source_DVDRip >= 7)
      RELEASE = %RELEASE%DVDRip.
   
   ;~ Get CODEC!
   CODEC_XVID := RegExMatch( Filename, "i)[\._[:space:]]xvid[\._[:space:]-]")
   If (CODEC_XVID >= 12)
      RELEASE = %RELEASE%XviD-

   CODEC_X264 := RegExMatch( Filename, "i)[\._[:space:]]x264[\._[:space:]-]")
   If (CODEC_X264 >= 12)
      RELEASE = %RELEASE%x264-

   CODEC_DIVX := RegExMatch( Filename, "i)[\._[:space:]]divx[\._[:space:]-]")
   If (CODEC_DIVX >= 12)
      RELEASE = %RELEASE%DivX-
 
   TAGSOFFSET := CODEC_XVID+CODEC_DIVX+CODEC_X264
   RegExMatch( Filename, "i)[\._[:space:]-].+\.", GROUP, TAGSOFFSET+1)
   StringTrimRight, GROUP, GROUP, 1
   StringTrimLeft, GROUP, GROUP, 1
   StringUpper, GROUP, GROUP  
   RELEASE = %RELEASE%%GROUP%
 
   ;MsgBox, DVDRip = %Source_DVDRip%`nXviD = %CODEC_XVID%`nDivX = %CODEC_DIVX%`nOFFSET = %OFFSET%`nGROUP = %GROUP%`nSUFFIX = %SUFFIX%
 
   Return RELEASE
}
;=================================================================


;Provide a show name (greek) and it will return the TVDB data for the show name (Greek) and series ID (80301) as a CSV (Greek,80301)
GetTVDBShows(ShowName)
{
   Global SeriesName, SeriesIDNum
   IfExist, Series_ID.xml
      FileDelete, Series_ID.xml
   UrlDownloadToFile, http://www.thetvdb.com/api/GetSeries.php?seriesname="%ShowName%", Series_ID.xml
   XPath_Load(xml, "Series_ID.xml")
   FileDelete, Series_ID.xml
   SeriesNameNodeCount := XPath(xml, "/Data/Series/SeriesName/Count()")
   If SeriesNameNodeCount = 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
   }
   Else If SeriesNameNodeCount > 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
      SeriesOverview := XPath(xml, "/Data/Series/Overview/text()")
      StringSplit, OutputArraySID, SeriesIDNum, `,
      StringSplit, OutputArraySN, SeriesName, `,
      StringSplit, OutputArraySO, SeriesOverview, `,
      Gui, 2: Default
      Gui, 2: +ToolWindow +Owner1
      Gui, 2: Add, ListView, xm r15 w600, Show name|Show description|TVDB series ID no.
      Gui, 2: Add, Text, y+7
      Gui, 2: Add, Button, x+475, Select
      Gui, 2: Add, Button, x+15, Cancel
      Loop, %SeriesNameNodeCount%
      {
         SeriesIDNum := OutputArraySID%A_Index%
         SeriesName := OutputArraySN%A_Index%
         SeriesOverview := OutputArraySO%A_Index%
         IfInString, SeriesOverview, ,`;
            StringReplace, SeriesOverview, SeriesOverview, ,`;, `,, All
         If not SeriesOverview
            SeriesOverview = (No show description provided)
         LV_Add(Row, SeriesName, SeriesOverview, SeriesIDNum)
         LV_ModifyCol()
         LV_ModifyCol(3, 110)
         SeriesIDNum =
         SeriesName =
         SeriesOverview =
      }
      Gui, 2: Show,,Which Show?
      Gui, 1: Default
      WinWaitClose, Which Show?
   }
   If not SeriesName
      SeriesName = %ShowName%
   J = %SeriesName%,%SeriesIDNum%
   ShowName =
   SeriesIDNum =
   SeriesName =
   SeriesOverview =
   Return J
}

   ;Labels required for Gui 2
   2GuiEscape:
   2GuiClose:
   2ButtonCancel:
   {
      Gui, 2: Destroy
      Return
   }
   2ButtonSelect:
   {
      FocusedRowNumber := LV_GetNext(0, "F")
      If not FocusedRowNumber
      {
         MsgBox, 0, Warning!, You must select a show or press cancel!
         Return
      }
      LV_GetText(SeriesName, FocusedRowNumber, 1)
      LV_GetText(SeriesIDNum, FocusedRowNumber, 3)
      FocusedRowNumber =
      Gui, 2: Destroy
      Return
   }

;Provide the "TVDB series ID", "season number" and "episode number" and this will return the TVDB season number, episode number, and episode title as a CSV (3,1,The Day After)
GetTVDBEpisodes(SeriesIDNum, SeasonNum, EpisodeNum, ShowName)
{
   GuiControlGet, CacheInfo,, Cache
   FileName = data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml
   If CacheInfo = 1
   {
      IfNotExist, FileName
         UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, %FileName%
      If not XPath_Load(xml, FileName)
         Msgbox, 48, [XPath]  , Load of %FileName% Failed!
   }
   Else
   {
      IfExist, Episode_Info.xml
         FileDelete, Episode_Info.xml
      UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, Episode_Info.xml
      If not XPath_Load(xml, "Episode_Info.xml")
         Msgbox, 48, [XPath]  , Load of "Episode_Info.xml" Failed!
      FileDelete, Episode_Info.xml
   }
   SeasonNumber := XPath(xml, "/Data/Episode/SeasonNumber/text()")
   EpisodeNumber := XPath(xml, "/Data/Episode/EpisodeNumber/text()")
   EpisodeTitle := XPath(xml, "/Data/Episode/EpisodeName/text()")
   If not EpisodeTitle
   {
      IfExist, %FileName%
         FileDelete, %FileName%
   }
   If not SeasonNumber
      SeasonNumber = %SeasonNum%
   If not EpisodeNumber
      EpisodeNumber = %EpisodeNum%
   K = %SeasonNumber%,%EpisodeNumber%,%EpisodeTitle%
   SeasonNumber =
   EpisodeNumber =
   EpisodeTitle =
   Return K
}

;Pads a variable with a preceeding zero
PadZero(VarToPad)
{
   VarToPadLen := StrLen(VarToPad)
   If VarToPadLen = 1
      VarToPad = 0%VarToPad%
   VarToPadLen =
   Return VarToPad
}

/*
>>>>CHANGELOG<<<<

1.0 (10-5-2009)
   - Original script
1.1 (10-6-2009)
   - Fixed a bug with adding a single file where the file would not be renamed
   -- Forgot to define the TargetFile's TargetFolder when updating the GUI
1.2 (10-13-2009)
   - Fixed another bug with adding a single file where the file would not be renamed
   - Fixed a bug with parsing the series overview where the non-comma delimiter (,) would be displayed
   -- Titans XPath replaces commas with "," (no quotes)
1.30 (11-8-2009)
   - Added a new method of selecting a show name when multiple results are returned
   -- Secondary ListView gui is displayed allowing the user to select the correct show
   - Script now compares the current filename against the previous filename (filenames converted to UPPERCASE)
   -- Drasitically reduces the number of requests against the TVDB and speeds everything up when renaming multiple shows all with the same name
   - Added a check and replace system for invalid filename characters
   -- StringReplace these chars \ / : * ? "" < > | with descriptive replacements
   - Fixed a bug in the GetEpisodeNumber() function
   -- A single digit season number would sometimes cause too few of the episode numbers to be returned when SeasonAndEpisode contained (e|x)
   - Tweaked GetSeasonNumber() and GetEpisodeNumber() to more reliably return correct data
   -- StringGetPos and String(Left|Right) were swapped for StringSplit to more predictably split SeasonAndEpisode regardless of (e|x) char location
   - Added a check to make sure files have a new filename in the preview column before renaming
   - Added a static window title which may be needed for various features in the future as well as making things look nicer :P
1.40 (11-15-2009)
   - Added status bar
   - Fixed a bug where a file would not have a preceeding zero added if no info was found on TheTVDB by adding PadZero() function
   -- Preceeding zero was only added in GetTVDBEpisodes() which means that if SeriesIDNo was blank (i.e. no info pulled from TheTVDB) the Season and Episode number would not get padded

1.40a (12-11-2009) by Pacheco
   - Added an option to preserve the information of [Source.Codec-Group] if present

1.41 (12-10-2009)
   - Fixed a serious bug where no results would be returned for ShowName inside the GetTVDBShows function would return the first result infinitely
   -- Had to clear the SeriesName var at the end of the function so that on the next loop iteration the correct var contents would exist
1.42 (12-11-2009)
   - Fixed a minor bug where the second time the GetTVDBInfo button was pressed, the first item in the list view would not actually be rescanned
   -- Had to clear the UpperPrevSN and PrevShowName vars so that the second time the button was pressed it would compare the correct names

>>>>Paradox Changelog<<<<

1.42.1 (2-5-2010)
   - Added new checkbox and supporting code that allows you to save the xmls downloaded in data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml to make future renames quicker.
   -- Added check that determines if you are connected to the internet and thetvdb.com is reachable
   - Added better handling for previous showname
   -- Clean up some code that caused shows to use the previous renamed file settings wrongly
   - Renamed GetShowName(FileName) to GetName(FileName) and added new GetShowName(FileName, FileDir) to implement the following feature without requiring big changes to existing scripts.
   -- Added GetNameFromDir(FileName, Directory) to handle using the directory name as the showname, in case you are renaming files named 01x01 - title.avi the directory must, of course, be named as the show name.
   - Added debug message box if the episode info xml can't be read for some reason.
   -- Fixed Bug where first file would be properly renamed "Castle [2009] - 01x01 - Title" and the rest would be improperly renamed Castle - 01x02 - Title"
   - I am planning to add more features including using the API properly to download All the Episode information for a show and cache it locally/update on a regular basis.
   -- Also Planning to add a feature that will recognize when you are missing episodes from a season and either create a .txt file with the show name, season, episode, and title in the directory, or, keep track of it in a log file of some sort.
1.42.2 (2-5-2010)
   - Added support for choosing if you want to use spaces, periods or underscores as separators
   -- Added A checkbox that allows you to clear the list after renaming, so you don't have to click the button :)
   - Moved some things on the interface around a bit to accommodate previous features.
   -- Fixed an error where the list of files in the directory were being displayed after renaming instead of the previous items, renamed.
   - Added an option that allows you to click on rename and have it do the TVDB lookup and rename all in one (Be careful with this option)
   -- Fix up a few other small bugs
1.42.3 (2-5-2010)
   - Remove Some debug code I forgot to remove before.
   -- Save original title (The part between the 01x01 and the extension), if any to be added back later if no official title is found.
   - Implement better way of refreshing the display after a rename.
1.42.4 (2-5-2010)
   - Fix Typo that would keep files with no . in them at all from being renamed properly. (Exists in original 1.42 script as well)
1.42.5 (2-6-2010)
   - Fix renaming files with no Season and Episode Number (use original filename)
1.42.5a (2-8-2010)
  - Re-added code to read/keep the info of the release version... [by Pacheco]
1.42.6 (2-9-2010)
  - Fixed a typo where a closing curly brace } was in the wrong place.
1.42.7 (2-9-2010)
  - Fixed handling of files without release info such as group and format.
1.42.8 (2-17-2010)
  - Re-written the Release info fetching (now with regex), and added some more release tags
*/


Delusion
  • Members
  • 272 posts
  • Last active: Jul 13 2014 09:04 PM
  • Joined: 16 Jul 2008
it would be nice if someone could make a similar script to work with movies instead of series
or maybe both?

this api looks pretty nice : <!-- m -->http://api.themoviedb.org/2.1/<!-- m -->
but i have to study a bit more on how things work with xpath etc
QuickSubs | Popcorn Movie Catalog
All my scripts are just in AutoHotkey v1.0.48.05

Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
Sorry, but i don't see the problem with the movies?

Don't they already have the full name on the release?
In the series, normally the name of the episode is missing, but in movies?

Don't get your point... can you explain a little better your intention?

Pacheco
  • Members
  • 168 posts
  • Last active: Apr 11 2013 08:21 AM
  • Joined: 23 Feb 2006
Here is another release: 1.42.9

Changes:

1.42.9 (5-5-2010) - (by Pacheco)
- Corrected a bug with some release tags (missing the "dot")
- Added the option of drag'n'drop [multiple] files directly to the window


; SceneRenamer
; Modified By Paradox iamparadox at netscape.net
; Added RELEASE feature by Pacheco
; AutoHotkey Version: 1.0.48+
;
; Script Function:
;   Extracts season and episode info from the filename
;   and uses that to retreive info from TheTVDB.com to rename
;   the file to a user-specified format

;   Special thanks to TheTVDB.com
;   This project would not have been possible without their top-notch API

#Include xpath.ahk

#NoEnv
SendMode, Input
SetWorkingDir, %A_ScriptDir%

WindowTitle = SceneRenamer v1.42.9 Modified By Paradox & Pacheco

Gui, Add, Button, Default gButtonAddFolder, &Add folder
Gui, Add, Button, x+20 gButtonAddFile, &Add file
Gui, Add, Button, x+20 gButtonClear, &Clear list
Gui, Add, Text, x+150 w50, Status:
Gui, Add, Edit, +ReadOnly x+0 w400, Waiting for user to add file(s)...

Gui, Add, ListView, xm r26 w800 -ReadOnly vMyListView, Name|Preview|Directory|Extension|TVDB Series ID No

Gui, Add, DropDownList, w300 AltSubmit vNameScheme, (Show Title) - (SS)x(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title)|(SS)x(EE) - Episode Title|S(SS)E(EE) - (Episode Title)|(Show Title) - S(SS)E(EE) - (Episode Title) - (Release)||
Gui, Add, DropDownList, x+10 w180 AltSubmit vSeparator, Use spaces as separators||Use.periods.as.separators|Use_underscores_as_separators
Gui, Add, Button, x+100 gButtonGetTVDBInfo, &Get TVDB Info
Gui, Add, Button, x+10 gButtonRename, &Rename files

Gui, Add, Checkbox, xm Checked vZero, Pad with zero?
Gui, Add, Checkbox, x+10 vCache, Cache Episode Info?
Gui, Add, Checkbox, x+10 vRename, Allow Direct Rename?
Gui, Add, Checkbox, x+10 Checked vClear, Clear List after Rename?

Menu, MyContextMenu, Add, Open, ContextOpenFile
Menu, MyContextMenu, Add, Properties, ContextProperties
Menu, MyContextMenu, Add, Remove item, ContextRemoveItem

Gui, Show,, %WindowTitle%
Return

GuiClose:
   ExitApp

GuiContextMenu:
   If A_GuiControl <> MyListView
       Return
   Menu, MyContextMenu, Show, %A_GuiX%, %A_GuiY%
   Return

ContextOpenFile:
ContextProperties:
   FocusedRowNumber := LV_GetNext(0, "F")
   If not FocusedRowNumber
      return
   LV_GetText(FileName, FocusedRowNumber, 1)
   LV_GetText(FileDir, FocusedRowNumber, 3)
   IfInString A_ThisMenuItem, Open
      Run %FileDir%\%FileName%,, UseErrorLevel
   Else
      Run Properties "%FileDir%\%FileName%",, UseErrorLevel
   If ErrorLevel
      MsgBox Could not perform requested action on "%FileDir%\%FileName%".
   return

ContextRemoveItem:
   RowNumber = 0
   Loop
   {
      RowNumber := LV_GetNext(RowNumber - 1)
      If not RowNumber
           Break
      LV_Delete(RowNumber)
   }
   Return

ButtonAddFolder:
   Gui +OwnDialogs
   FileSelectFolder, TargetFolder,, 3, Select a folder:
   If not TargetFolder  ; The user pressed cancel
       Return
   StringRight, LastChar, TargetFolder, 1
   If LastChar = \
      StringTrimRight, TargetFolder, TargetFolder, 1
   GuiControl, -Redraw, MyListView ; Improves performance by disabling redrawing
   Loop %TargetFolder%\*.*
   {
      FileName := A_LoopFileFullPath
      SplitPath, FileName,,, FileExt  ; Get the file extension
      If FileExt in avi,mkv,mp4,srt
         LV_Add(Row, A_LoopFileName, "", A_LoopFileDir, FileExt)
   }
   GuiControl, +Redraw, MyListView  ; Turn redrawing back on
   LV_ModifyCol()  ; Resize each column to fit
   LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
   LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
   LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   ControlSetText, Edit1, Added folder %TargetFolder%, %WindowTitle%
   Return

ButtonAddFile:
   Gui +OwnDialogs
   FileSelectFile, TargetFile
   If not TargetFile
      Return
   SplitPath, TargetFile,,, FileExt  ; Get the file extension
   If FileExt in avi,mkv,mp4,srt
   {
      SplitPath, TargetFile, FileName, TargetFolder
      LV_Add(Row, FileName, "", TargetFolder, FileExt)
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      ControlSetText, Edit1, Added file %TargetFile%..., %WindowTitle%
   }
   Return
   
GuiDropFiles:
   Gui +OwnDialogs
   Loop, parse, A_GuiEvent, `n
   {
      TargetFile = %A_LoopField%
      SplitPath, TargetFile,,, FileExt  ; Get the file extension
      If FileExt in avi,mkv,mp4,srt
      {
         SplitPath, TargetFile, FileName, TargetFolder
         LV_Add(Row, FileName, "", TargetFolder, FileExt)
         LV_ModifyCol()  ; Resize each column to fit
         LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
         LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
         LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
         ControlSetText, Edit1, Added file %TargetFile%..., %WindowTitle%
      }
   }
Return

ButtonClear:
   ClearList()
   Return

ClearList()
{
   TargetFolder =
   TargetFile =
   LV_Delete()
   ControlSetText, Edit1, Cleared Listview, %WindowTitle%
   Return
}

InternetCheckConnection(Url="",FIFC=1) {
   Return DllCall("Wininet.dll\InternetCheckConnectionA", Str,Url, Int,FIFC, Int,0)
}

ButtonGetTVDBInfo:
   GetTVDBInfo()
   Return

GetTVDBInfo()
{
   LV_GetText(LVFileName, 1, 1)
   If not LVFileName
   {
      Gui +OwnDialogs
      MsgBox, 0,Fatal Error!, You must select a file or folder before connecting to TheTVDB.com!
      Return
   }
   LV_GetText(LVFileDir, 1, 3)  ; Get the file directory
   LV_GetText(LVFileExt, 1, 4)  ; Get the file extension

   URL := "http://www.thetvdb.com"

   If not InternetCheckConnection(URL)
   {
      Msgbox, 48, [%URL%]  , Connection Failed!
      Return
   }

   RowNumber = 1
   ShowInfo := GetTVDBShows(GetShowName(LVFileName, LVFileDir))
   StringSplit, ShowArray, ShowInfo, `,
   ShowName = %ShowArray1%
   SeriesIDNo = %ShowArray2%
   Loop
   {
      Gui +OwnDialogs
      If A_Index > 1
      {
         LV_GetText(LVPrevFN, RowNumber - 1, 1)  ; Get the preceeding rows filename
         LV_GetText(LVPrevFD, RowNumber - 1, 3)  ; Get the preceeding rows file directory
         PrevShowName := GetShowName(LVPrevFN, LVPrevFD)
         StringUpper, UpperPrevSN, PrevShowName

         NewShowName := GetShowName(LVFileName, LVFileDir)
         StringUpper, UpperCurrSN, NewShowName
         If UpperCurrSN <> %UpperPrevSN%
         {
            ShowInfo := GetTVDBShows(NewShowName)
            StringSplit, ShowArray, ShowInfo, `,
            ShowName = %ShowArray1%
            SeriesIDNo = %ShowArray2%
         }
      }
      UpperPrevSN =
      SeasonNumber := GetSeasonNumber(LVFileName)
      EpisodeNumber := GetEpisodeNumber(LVFileName)
      OriginalTitle := GetTitle(LVFileName)
      ReleaseInfo := GetRelease(LVFileName)
      ControlSetText, Edit1, Processing "%ShowName%" Season "%SeasonNumber%" Episode "%EpisodeNumber%", %WindowTitle%
      If SeriesIDNo
      {
         IfInString, ShowName, %A_Space%
            StringReplace, SafeShowName, ShowName, %A_Space%, _, All
         Else
            SafeShowName = %ShowName%
         EpisodeInfo := GetTVDBEpisodes(SeriesIDNo, SeasonNumber, EpisodeNumber, SafeShowName)
         StringSplit, OutputArray, EpisodeInfo, `,
         SeasonNumber = %OutputArray1%
         EpisodeNumber = %OutputArray2%
         EpisodeTitle = %OutputArray3%
         If EpisodeTitle
         {
            StringReplace, EpisodeTitle, EpisodeTitle, \, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, /, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, :, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, *, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, ?,, All
            StringReplace, EpisodeTitle, EpisodeTitle, ", ', All
            StringReplace, EpisodeTitle, EpisodeTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, |, %A_Space%~%A_Space%, All
            StringReplace, EpisodeTitle, EpisodeTitle, &`#44;, `,, All
            StringReplace, EpisodeTitle, EpisodeTitle, &, &, All
         }
         If OriginalTitle
         {
            StringReplace, OriginalTitle, OriginalTitle, \, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, /, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, :, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, *, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, ?,, All
            StringReplace, OriginalTitle, OriginalTitle, ", ', All
            StringReplace, OriginalTitle, OriginalTitle, <, %A_Space%less than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, >, %A_Space%greater than%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, |, %A_Space%~%A_Space%, All
            StringReplace, OriginalTitle, OriginalTitle, &`#44;, `,, All
            StringReplace, OriginalTitle, OriginalTitle, &, &, All
         }
         GuiControlGet, PadZero,, Zero
         If PadZero = 1
         {
            SeasonNumber := PadZero(SeasonNumber)
            EpisodeNumber := PadZero(EpisodeNumber)
         }
         GuiControlGet, NamingScheme,, NameScheme
         If NamingScheme = 1
         {
            If EpisodeTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - %SeasonNumber%x%EpisodeNumber%
         }
         Else if NamingScheme = 2
         {
            If EpisodeTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 3
         {
            If EpisodeTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = %SeasonNumber%x%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = %SeasonNumber%x%EpisodeNumber%
         }
         Else If NamingScheme = 4
         {
            If EpisodeTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
            Else If OriginalTitle
               NewFileName = S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
            Else
               NewFileName = S%SeasonNumber%E%EpisodeNumber%
         }
         Else If NamingScheme = 5
         {
            If StrLen(ReleaseInfo) > 0
            {
               If EpisodeTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle% - %ReleaseInfo%
               Else If OriginalTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle% - %ReleaseInfo%
               Else
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %ReleaseInfo%
            }
            Else
            {
               If EpisodeTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %EpisodeTitle%
               Else If OriginalTitle
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber% - %OriginalTitle%
               Else
                  NewFileName = %ShowName% - S%SeasonNumber%E%EpisodeNumber%
            }
         }
      }
      Else
      {
         StringReplace, LVFN, LVFileName, \, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, /, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, :, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, *, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, ?,, All
         StringReplace, LVFN, LVFN, ", ', All
         StringReplace, LVFN, LVFN, <, %A_Space%less than%A_Space%, All
         StringReplace, LVFN, LVFN, >, %A_Space%greater than%A_Space%, All
         StringReplace, LVFN, LVFN, |, %A_Space%~%A_Space%, All
         StringReplace, LVFN, LVFN, &`#44;, `,, All
         StringReplace, LVFN, LVFN, &, &, All
         StringTrimRight, LVFN, LVFN, 4
         Loop
         {
            StringRight, LastChar, LVFN, 1
            If (LastChar = "-" or LastChar = " ")
               StringTrimRight, LVFN, LVFN, 1
            Else
               Break
         }
         NewFileName = %LVFN%
      }
      GuiControlGet, Sep,, Separator
      If Sep = 2
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, ., All
      }
      Else If Sep = 3
      {
         StringReplace, NewFileName, NewFileName, %A_Space%, _, All
      }
      NewFileName = %NewFileName%.%LVFileExt%
      LV_Modify(RowNumber, Col1, LVFileName, NewFileName, LVFileDir, LVFileExt, SeriesIDNo)
      LV_ModifyCol()
      LV_ModifyCol(4, 60)
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
      LVFileName =
      LVPrevFN =
      ShowArray1 =
      OutputArray1 =
      OutputArray2 =
      OutputArray3 =
      SeasonNumber =
      EpisodeNumber =
      EpisodeTitle =
      OriginalTitle =
      RowNumber := RowNumber + 1
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the filename
      If not LVFileName  ; Break the loop at the end of the list
         Break
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file directory
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
   }
   ControlSetText, Edit1, Finished querying TheTVDB.com. Ready to rename..., %WindowTitle%
   Return
}

ButtonRename:
{
   LV_GetText(LVNewFileName, 1, 2)
   If not LVNewFileName
   {
      GuiControlGet, DirectRename,, Rename
      If DirectRename = 1
      {
         GetTVDBInfo()
      }
      Else
      {
         Gui +OwnDialogs
         MsgBox, 0,Fatal Error!, You must scan your files against TheTVDB.com before you rename them!
         Return
      }
   }
   Loop
   {
      RowNumber = %A_Index%
      LV_GetText(LVFileName, RowNumber, 1)  ; Get the original filename
      LV_GetText(LVNewFileName, RowNumber, 2)  ; Get the new filename
      LV_GetText(LVFileDir, RowNumber, 3)  ; Get the file dir
      LV_GetText(LVFileExt, RowNumber, 4)  ; Get the file extension
      If not LVNewFileName  ; Break the loop at the end of the list
         Break
      ControlSetText, Edit1, Renaming "%LVFileName%" to "%LVNewFileName%", %WindowTitle%
      FileMove, %LVFileDir%\%LVFileName%, %LVFileDir%\%LVNewFileName%  ; Finally, rename it
      LV_Modify(RowNumber, Col1, LVNewFileName, "", LVFileDir, LVFileExt, SeriesIDNo)
   }
   RenameCount := RowNumber - 1
   ControlSetText, Edit1, Finished renaming files! Refreshing file list., %WindowTitle%
   GuiControlGet, ClearIt,, Clear
   If ClearIt = 1
   {
      ClearList()
   }
   Else
   {
      GuiControl, +Redraw, MyListView  ; Turn redrawing back on
      LV_ModifyCol()  ; Resize each column to fit
      LV_ModifyCol(2, 60)  ; Expand "Preview" to keep the header visible
      LV_ModifyCol(4, 60)  ; Expand "Extension" to keep the header visible
      LV_ModifyCol(5, 110)  ; Expand "TVDB Series ID No" to keep the header visible
   }
   ControlSetText, Edit1, Finished Successfully!, %WindowTitle%
   Return
}

;Provide a filename (greek.s01e01.original.title.avi) and this should return the original title (in case a title isn't found)
GetTitle(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      NumberOfChars := (StrLen(FileName)-(StrLen(SeasonAndEpisode)+SeasonAndEpisodePos))
      StringRight, ShowTitle, FileName, NumberOfChars
   }
   Loop
   {
      StringRight, LastChar, ShowTitle, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Loop
   {
      StringLeft, FirstChar, ShowTitle, 1
      If (FirstChar = "-" or FirstChar = " ")
         StringTrimLeft, ShowTitle, ShowTitle, 1
      Else
         Break
   }
   Return ShowTitle
}

;Provide a filename (s01e01.avi or greek.s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetShowName(FileName, FileDir)
{
   TempName := GetName(FileName)
   If not TempName
      TempName := GetNameFromDir(FileName, FileDir)
   Return TempName
}
;Provide a filename (greek.s01e01.avi) and this should return the show name
GetName(FileName)
{
   SplitPath , FileName, name, dir, ext, FileName
   IfInString, FileName, .
      StringReplace, FileName, FileName, ., %A_Space%, All
   IfInString, FileName, _
      StringReplace, FileName, FileName, _, %A_Space%, All
   If RegExMatch(FileName, "i)s[0-9][0-9]? ?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringGetPos, SeasonAndEpisodePos, FileName, %SeasonAndEpisode%
      StringLeft, ShowName, FileName, (SeasonAndEpisodePos - 1)
   }
   Loop
   {
      StringRight, LastChar, ShowName, 1
      If (LastChar = "-" or LastChar = " ")
         StringTrimRight, ShowName, ShowName, 1
      Else
         Break
   }
   If SeasonAndEpisodePos > -1
      Return ShowName
   Else
   {
      Return FileName
   }
}

;Provide a filename (s01e01.avi) and Directory name (C:\TV\greek) and this should return the show name
GetNameFromDir(FileName, FileDir)
{
   Directory = %FileDir% - %FileName%
   SplitPath , Directory, NewFileName, dir, ext, name
   Return GetName(NewFileName)
}

;Provide a filename (greek.s01e01.avi) and this should return the season number
GetSeasonNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }

   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      SeasonNumber = %OutputArray1%
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringTrimRight, SeasonNumber, SeasonAndEpisode, 2
      IfInString, SeasonNumber, 0
      {
         StringGetPos, ZeroLoc, SeasonNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, SeasonNumber, SeasonNumber, 1
      }
   }
   Return SeasonNumber
}

;Provide a filename (greek.s01e01.avi) and this should return the episode number
GetEpisodeNumber(FileName)
{
   If RegExMatch(FileName, "i)s\K[0-9][0-9]?.?e[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, e E, .
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "i)[0-9][0-9]?.?x[0-9][0-9]?", SeasonAndEpisode)
   {
      StringSplit, OutputArray, SeasonAndEpisode, x X
      EpisodeNumber = %OutputArray2%
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Else If RegExMatch(FileName, "[0-9][0-9][0-9][0-9]?", SeasonAndEpisode)
   {
      StringRight, EpisodeNumber, SeasonAndEpisode, 2
      IfInString, EpisodeNumber, 0
      {
         StringGetPos, ZeroLoc, EpisodeNumber, 0
         If ZeroLoc = 0
            StringTrimLeft, EpisodeNumber, EpisodeNumber, 1
      }
   }
   Return EpisodeNumber
}

;============================== RELEASE ==========================
GetRelease(FileName)
{
   ; Stores the source, codec and group to use latter
   RELEASE =

   ; FoundPos := RegExMatch( VAR, "i)[\._[:space:]]hdtv[\._[:space:]]")

   ;~ Check EXTRA tags
   WS := RegExMatch( Filename, "i)[\._[:space:]]ws[\._[:space:]]")
   If (WS >= 7)
      RELEASE = %RELEASE%WS.

   EXTENDED := RegExMatch( Filename, "i)[\._[:space:]]extended[\._[:space:]]")
   If (EXTENDED >= 7)
      RELEASE = %RELEASE%EXTENDED.

   INTERNAL := RegExMatch( Filename, "i)[\._[:space:]]internal[\._[:space:]]")
   If (INTERNAL >= 7)
      RELEASE = %RELEASE%iNTERNAL.
      
   PREAIR := RegExMatch( Filename, "i)[\._[:space:]]preair[\._[:space:]]")
   If (PREAIR >= 7)
      RELEASE = %RELEASE%PREAIR.

   FINAL := RegExMatch( Filename, "i)[\._[:space:]]final[\._[:space:]]")
   If (FINAL >= 7)
      RELEASE = %RELEASE%FINAL.

   REPACK := RegExMatch( Filename, "i)[\._[:space:]]repack[\._[:space:]]")
   If (REPACK >= 7)
      RELEASE = %RELEASE%REPACK.

   PROPER := RegExMatch( Filename, "i)[\._[:space:]]proper[\._[:space:]]")
   If (PROPER >= 7)
      RELEASE = %RELEASE%PROPER.
   
   ;~ Get SOURCE!
   Source_720p := RegExMatch( Filename, "i)[\._[:space:]]720p[\._[:space:]]")
   If (Source_720p >= 7)
      RELEASE = %RELEASE%720p.

   Source_1080p := RegExMatch( Filename, "i)[\._[:space:]]1080p[\._[:space:]]")
   If (Source_1080p >= 7)
      RELEASE = %RELEASE%1080p.

   Source_1080i := RegExMatch( Filename, "i)[\._[:space:]]1080i[\._[:space:]]")
   If (Source_1080i >= 7)
      RELEASE = %RELEASE%1080i.

   Source_HDTV := RegExMatch( Filename, "i)[\._[:space:]]hdtv[\._[:space:]]")
   If (Source_HDTV >= 7)
      RELEASE = %RELEASE%HDTV.

   HR := RegExMatch( Filename, "i)[\._[:space:]]hr[\._[:space:]]")
   If (HR >= 7)
      RELEASE = %RELEASE%HR.

   Source_PDTV := RegExMatch( Filename, "i)[\._[:space:]]pdtv[\._[:space:]]")
   If (Source_PDTV >= 7)
      RELEASE = %RELEASE%PDTV.

   Source_DVDRip := RegExMatch( Filename, "i)[\._[:space:]]dvdrip[\._[:space:]]")
   If (Source_DVDRip >= 7)
      RELEASE = %RELEASE%DVDRip.
   
   ;~ Get CODEC!
   CODEC_XVID := RegExMatch( Filename, "i)[\._[:space:]]xvid[\._[:space:]-]")
   If (CODEC_XVID >= 12)
      RELEASE = %RELEASE%XviD-

   CODEC_X264 := RegExMatch( Filename, "i)[\._[:space:]]x264[\._[:space:]-]")
   If (CODEC_X264 >= 12)
      RELEASE = %RELEASE%x264-

   CODEC_DIVX := RegExMatch( Filename, "i)[\._[:space:]]divx[\._[:space:]-]")
   If (CODEC_DIVX >= 12)
      RELEASE = %RELEASE%DivX-
 
   TAGSOFFSET := CODEC_XVID+CODEC_DIVX+CODEC_X264
   RegExMatch( Filename, "i)[\._[:space:]-].+\.", GROUP, TAGSOFFSET+1)
   StringTrimRight, GROUP, GROUP, 1
   StringTrimLeft, GROUP, GROUP, 1
   StringUpper, GROUP, GROUP  
   RELEASE = %RELEASE%%GROUP%
 
   ;MsgBox, DVDRip = %Source_DVDRip%`nXviD = %CODEC_XVID%`nDivX = %CODEC_DIVX%`nOFFSET = %OFFSET%`nGROUP = %GROUP%`nSUFFIX = %SUFFIX%
 
   Return RELEASE
}
;=================================================================


;Provide a show name (greek) and it will return the TVDB data for the show name (Greek) and series ID (80301) as a CSV (Greek,80301)
GetTVDBShows(ShowName)
{
   Global SeriesName, SeriesIDNum
   IfExist, Series_ID.xml
      FileDelete, Series_ID.xml
   UrlDownloadToFile, http://www.thetvdb.com/api/GetSeries.php?seriesname="%ShowName%", Series_ID.xml
   XPath_Load(xml, "Series_ID.xml")
   FileDelete, Series_ID.xml
   SeriesNameNodeCount := XPath(xml, "/Data/Series/SeriesName/Count()")
   If SeriesNameNodeCount = 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
   }
   Else If SeriesNameNodeCount > 1
   {
      SeriesIDNum := XPath(xml, "/Data/Series/seriesid/text()")
      SeriesName := XPath(xml, "/Data/Series/SeriesName/text()")
      SeriesOverview := XPath(xml, "/Data/Series/Overview/text()")
      StringSplit, OutputArraySID, SeriesIDNum, `,
      StringSplit, OutputArraySN, SeriesName, `,
      StringSplit, OutputArraySO, SeriesOverview, `,
      Gui, 2: Default
      Gui, 2: +ToolWindow +Owner1
      Gui, 2: Add, ListView, xm r15 w600, Show name|Show description|TVDB series ID no.
      Gui, 2: Add, Text, y+7
      Gui, 2: Add, Button, x+475, Select
      Gui, 2: Add, Button, x+15, Cancel
      Loop, %SeriesNameNodeCount%
      {
         SeriesIDNum := OutputArraySID%A_Index%
         SeriesName := OutputArraySN%A_Index%
         SeriesOverview := OutputArraySO%A_Index%
         IfInString, SeriesOverview, ,`;
            StringReplace, SeriesOverview, SeriesOverview, ,`;, `,, All
         If not SeriesOverview
            SeriesOverview = (No show description provided)
         LV_Add(Row, SeriesName, SeriesOverview, SeriesIDNum)
         LV_ModifyCol()
         LV_ModifyCol(3, 110)
         SeriesIDNum =
         SeriesName =
         SeriesOverview =
      }
      Gui, 2: Show,,Which Show?
      Gui, 1: Default
      WinWaitClose, Which Show?
   }
   If not SeriesName
      SeriesName = %ShowName%
   J = %SeriesName%,%SeriesIDNum%
   ShowName =
   SeriesIDNum =
   SeriesName =
   SeriesOverview =
   Return J
}

   ;Labels required for Gui 2
   2GuiEscape:
   2GuiClose:
   2ButtonCancel:
   {
      Gui, 2: Destroy
      Return
   }
   2ButtonSelect:
   {
      FocusedRowNumber := LV_GetNext(0, "F")
      If not FocusedRowNumber
      {
         MsgBox, 0, Warning!, You must select a show or press cancel!
         Return
      }
      LV_GetText(SeriesName, FocusedRowNumber, 1)
      LV_GetText(SeriesIDNum, FocusedRowNumber, 3)
      FocusedRowNumber =
      Gui, 2: Destroy
      Return
   }

;Provide the "TVDB series ID", "season number" and "episode number" and this will return the TVDB season number, episode number, and episode title as a CSV (3,1,The Day After)
GetTVDBEpisodes(SeriesIDNum, SeasonNum, EpisodeNum, ShowName)
{
   GuiControlGet, CacheInfo,, Cache
   FileName = data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml
   If CacheInfo = 1
   {
      IfNotExist, FileName
         UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, %FileName%
      If not XPath_Load(xml, FileName)
         Msgbox, 48, [XPath]  , Load of %FileName% Failed!
   }
   Else
   {
      IfExist, Episode_Info.xml
         FileDelete, Episode_Info.xml
      UrlDownloadToFile, http://www.thetvdb.com/api/563E39CDC7F666DF/series/%SeriesIDNum%/default/%SeasonNum%/%EpisodeNum%/en.xml, Episode_Info.xml
      If not XPath_Load(xml, "Episode_Info.xml")
         Msgbox, 48, [XPath]  , Load of "Episode_Info.xml" Failed!
      FileDelete, Episode_Info.xml
   }
   SeasonNumber := XPath(xml, "/Data/Episode/SeasonNumber/text()")
   EpisodeNumber := XPath(xml, "/Data/Episode/EpisodeNumber/text()")
   EpisodeTitle := XPath(xml, "/Data/Episode/EpisodeName/text()")
   If not EpisodeTitle
   {
      IfExist, %FileName%
         FileDelete, %FileName%
   }
   If not SeasonNumber
      SeasonNumber = %SeasonNum%
   If not EpisodeNumber
      EpisodeNumber = %EpisodeNum%
   K = %SeasonNumber%,%EpisodeNumber%,%EpisodeTitle%
   SeasonNumber =
   EpisodeNumber =
   EpisodeTitle =
   Return K
}

;Pads a variable with a preceeding zero
PadZero(VarToPad)
{
   VarToPadLen := StrLen(VarToPad)
   If VarToPadLen = 1
      VarToPad = 0%VarToPad%
   VarToPadLen =
   Return VarToPad
}

/*
>>>>CHANGELOG<<<<

1.0 (10-5-2009)
   - Original script
1.1 (10-6-2009)
   - Fixed a bug with adding a single file where the file would not be renamed
   -- Forgot to define the TargetFile's TargetFolder when updating the GUI
1.2 (10-13-2009)
   - Fixed another bug with adding a single file where the file would not be renamed
   - Fixed a bug with parsing the series overview where the non-comma delimiter (,) would be displayed
   -- Titans XPath replaces commas with "," (no quotes)
1.30 (11-8-2009)
   - Added a new method of selecting a show name when multiple results are returned
   -- Secondary ListView gui is displayed allowing the user to select the correct show
   - Script now compares the current filename against the previous filename (filenames converted to UPPERCASE)
   -- Drasitically reduces the number of requests against the TVDB and speeds everything up when renaming multiple shows all with the same name
   - Added a check and replace system for invalid filename characters
   -- StringReplace these chars \ / : * ? "" < > | with descriptive replacements
   - Fixed a bug in the GetEpisodeNumber() function
   -- A single digit season number would sometimes cause too few of the episode numbers to be returned when SeasonAndEpisode contained (e|x)
   - Tweaked GetSeasonNumber() and GetEpisodeNumber() to more reliably return correct data
   -- StringGetPos and String(Left|Right) were swapped for StringSplit to more predictably split SeasonAndEpisode regardless of (e|x) char location
   - Added a check to make sure files have a new filename in the preview column before renaming
   - Added a static window title which may be needed for various features in the future as well as making things look nicer :P
1.40 (11-15-2009)
   - Added status bar
   - Fixed a bug where a file would not have a preceeding zero added if no info was found on TheTVDB by adding PadZero() function
   -- Preceeding zero was only added in GetTVDBEpisodes() which means that if SeriesIDNo was blank (i.e. no info pulled from TheTVDB) the Season and Episode number would not get padded

1.40a (12-11-2009) by Pacheco
   - Added an option to preserve the information of [Source.Codec-Group] if present

1.41 (12-10-2009)
   - Fixed a serious bug where no results would be returned for ShowName inside the GetTVDBShows function would return the first result infinitely
   -- Had to clear the SeriesName var at the end of the function so that on the next loop iteration the correct var contents would exist
1.42 (12-11-2009)
   - Fixed a minor bug where the second time the GetTVDBInfo button was pressed, the first item in the list view would not actually be rescanned
   -- Had to clear the UpperPrevSN and PrevShowName vars so that the second time the button was pressed it would compare the correct names

1.42.1 (2-5-2010) [Paradox]
   - Added new checkbox and supporting code that allows you to save the xmls downloaded in data/%ShowName%_%SeasonNum%x%EpisodeNum%.xml to make future renames quicker.
   -- Added check that determines if you are connected to the internet and thetvdb.com is reachable
   - Added better handling for previous showname
   -- Clean up some code that caused shows to use the previous renamed file settings wrongly
   - Renamed GetShowName(FileName) to GetName(FileName) and added new GetShowName(FileName, FileDir) to implement the following feature without requiring big changes to existing scripts.
   -- Added GetNameFromDir(FileName, Directory) to handle using the directory name as the showname, in case you are renaming files named 01x01 - title.avi the directory must, of course, be named as the show name.
   - Added debug message box if the episode info xml can't be read for some reason.
   -- Fixed Bug where first file would be properly renamed "Castle [2009] - 01x01 - Title" and the rest would be improperly renamed Castle - 01x02 - Title"
   - I am planning to add more features including using the API properly to download All the Episode information for a show and cache it locally/update on a regular basis.
   -- Also Planning to add a feature that will recognize when you are missing episodes from a season and either create a .txt file with the show name, season, episode, and title in the directory, or, keep track of it in a log file of some sort.
1.42.2 (2-5-2010) [Paradox]
   - Added support for choosing if you want to use spaces, periods or underscores as separators
   -- Added A checkbox that allows you to clear the list after renaming, so you don't have to click the button :)
   - Moved some things on the interface around a bit to accommodate previous features.
   -- Fixed an error where the list of files in the directory were being displayed after renaming instead of the previous items, renamed.
   - Added an option that allows you to click on rename and have it do the TVDB lookup and rename all in one (Be careful with this option)
   -- Fix up a few other small bugs
1.42.3 (2-5-2010) [Paradox]
   - Remove Some debug code I forgot to remove before.
   -- Save original title (The part between the 01x01 and the extension), if any to be added back later if no official title is found.
   - Implement better way of refreshing the display after a rename.
1.42.4 (2-5-2010) [Paradox]
   - Fix Typo that would keep files with no . in them at all from being renamed properly. (Exists in original 1.42 script as well)
1.42.5 (2-6-2010) [Paradox]
   - Fix renaming files with no Season and Episode Number (use original filename)
1.42.5a (2-8-2010) [Paradox]
  - Re-added code to read/keep the info of the release version... [by Pacheco]
1.42.6 (2-9-2010) [Paradox]
  - Fixed a typo where a closing curly brace } was in the wrong place.
1.42.7 (2-9-2010) [Paradox]
  - Fixed handling of files without release info such as group and format.
1.42.8 (2-17-2010) [Pacheco]
  - Re-written the Release info fetching (now with regex), and added some more release tags 
1.42.9 (5-5-2010) [Pacheco]
  - Corrected a bug with some release tags (missing the "dot")
  - Added the option of drag'n'drop [multiple] files directly to the window
*/

Known Bugs:
- When a show has no episodes name in "TVDB", if the release exists, it duplicates that info as name of the episode.

-----------------------------------------------
Any suggestions or reporting bugs found is encouraged... :)