Powershell script to move Recorded TV (1 Viewer)

pbathuk

Portal Member
December 3, 2010
29
7
Chester
Home Country
United Kingdom United Kingdom
MediaPortal Version: 1.1.2
MediaPortal Skin: Dark-Maya
Windows Version: Windows 7 64bit

Hi all,

This is my first script which I have written for mediaportal, but thought I would share it as it may help others!
Basically my setup is HTPC in living room with smallish HDD and NAS with Lots of HDD's.
As I use Freesat and the only sockets into the house are near the HTPC that is my main server / client setup and due to the case I can't move my HDD into it.

So following the advise in another thread I have setup two Recorded (using the IPTV as the second recorded TV location).
** BTW I tried to setup my recorded TV to be on my NAS but it refused to record anything **

I have written the following which needs to be input into a powershell script. - I think you can save it into a txt file and then rename it to .ps1

The Powershell script needs to be edited with your own location for Start location / and end locations.

You need the the guide set up to have Film: infront of any description i.e. "Film: The Incredibles" as it uses the Film: to find out if it a film or not.

I have the powershell script linked to a BAT file so that I can run it via windows task scheduler at 3 am and 3pm. - If anyone has a better way to run this please let me know (I did try to link it to the comskiplauncher but it did not appear to work) - I can provide the BAT code to run this if needed!

Hopefully this will work for some of you...

Cheers
Phil

Code:
# Move Recorded TS Files to other locations

# Owner : Philip Bath
# Version : 0.2
# Notes : Set the StartLocation, FilmLocation and OtherLocation
#        The script then needs to be run via a BAT file
# Notes1 : You can also turn on / off (1 = on, 0 = off) to delete the XML files that will be left
#         In the recordedTV location, off moves it with the files

$StartLocation = "C:\Users\Public\Recorded TV\" # "C:\Users\Public\Recorded TV"
$FilmLocation = "\\NAS\VideoDownloads\"
$OtherLocation = "\\NAS\recorded tv\"
$DeleteFilm = 1
$DeleteOther = 0

# Build an array of the .TS files

$TSlist = Get-ChildItem -Path $StartLocation | Where-Object {$_.Name -like "*.ts"}
$Alllist = Get-ChildItem -Path $StartLocation | Where-Object {$_.Name -notlike "*.ts"}

# If the TSList is empty then stop here
    if (!$TSlist) 
    {
    }
    else
    {
        foreach ($TSfile in $TSlist)
        {
            # Check to see if the file starts with Film_
            if ($TSfile.Name.IndexOf("Film_") -ne -1)
            {
                foreach ($Allfile in $Alllist)
                {
                    $tmp = $TSfile.Name.IndexOf(".ts")
                    if ($Allfile.Name.IndexOf($TSfile.Name.SubString(0,$tmp)) -ne -1)
                    {
                        if ($DeleteOther -eq 1)
                        {
                            # Delete the file
                            Remove-Item $StartLocation$Allfile
                        }
                        else
                        {
                            # Move to the OtherLocation 
                            Rename-Item $StartLocation$AllFile $Allfile.Name.subString(5)
                            ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $OtherLocation.subString(0,$OtherLocation.Length-1) $Allfile.Name.subString(5) /MOV /R:2 /w:300 /NFL /NDL
                        }
                    }
                }
                # Now rename file (remove Film_ ) then
                Rename-Item $StartLocation$TSFile $TSfile.Name.subString(5)
                ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $FilmLocation.subString(0,$FilmLocation.Length-1) $TSfile.Name.subString(5) /MOV /R:2 /w:300 /NFL /NDL
            }
            else
            {
                # Section Used if not Film
                foreach ($Allfile in $Alllist)
                {
                    $tmp = $TSfile.Name.IndexOf(".ts")
                    if ($Allfile.Name.IndexOf($TSfile.Name.SubString(0,$tmp)) -ne -1)
                    {
                        if ($DeleteOther -eq 1)
                        {
                            # Delete the file
                            Remove-Item $StartLocation$Allfile
                        }
                        else
                        {
                            # Move to the OtherLocation 
                            ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $OtherLocation.subString(0,$OtherLocation.Length-1) $Allfile.Name /MOV /R:2 /w:300 /NFL /NDL
                        }
                    }
                }   
                # Move to the OtherLocation
                ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $OtherLocation.subString(0,$OtherLocation.Length-1) $TSfile.Name /MOV /R:2 /w:300 /NFL /NDL
            }
        }
    }
 

pbathuk

Portal Member
December 3, 2010
29
7
Chester
Home Country
United Kingdom United Kingdom
Hi,

I have re-written the script slightly so that it now takes into TV Series you already have on your PC.

Code:
# Move Recorded TS Files to other locations

# Owner : Philip Bath
# Version : 0.3
# Notes : Set the StartLocation, FilmLocation and OtherLocation
# Notes1 : You can also turn on / off (1 = on, 0 = off) to delete the XML files that will be left
#         In the recordedTV location, off means it will be left as is
# Notes2 : You can input the location of your TVSeriesLocation - This will ensure only series that you have in your
#         Collection will be moved.
#         Leave as "" if you don't want to use this feature

$StartLocation = "C:\Users\Public\Recorded TV\" # "C:\Users\Public\Recorded TV\"
$FilmLocation = "\\NAS\VideoDownloads\"
$OtherLocation = "\\NAS\VideoDownloads\"
$TVSeriesLocation = "\\NAS\Videos\TV Shows\"
$DeleteFilm = 1
$DeleteOther = 1

# Build an array of the .TS files

$TVShowlist = Get-ChildItem -Path $TVSeriesLocation | where {$_.mode -match "d"}
$TSlist = Get-ChildItem -Path $StartLocation | Where-Object {$_.Name -like "*.ts"}
$Alllist = Get-ChildItem -Path $StartLocation | Where-Object {$_.Name -like "*.xml"}

# If the TSList is empty then stop here
    if (!$TSlist) 
    {
    }
    else
    {
        foreach ($TSfile in $TSlist)
        {
            # Check to see if the file starts with Film_
            if ($TSfile.Name.IndexOf("Film_") -ne -1)
            {
                foreach ($Allfile in $Alllist)
                {
                    $tmp = $TSfile.Name.IndexOf(".ts")
                    if ($Allfile.Name.IndexOf($TSfile.Name.SubString(0,$tmp)) -ne -1)
                    {
                        if ($DeleteOther -eq 1)
                        {
                            # Delete the file
                            Remove-Item $StartLocation$Allfile
                        }
                        else
                        {
                            # Move to the OtherLocation 
                            Rename-Item $StartLocation$AllFile $Allfile.Name.subString(5)
                            ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $OtherLocation.subString(0,$OtherLocation.Length-1) $Allfile.Name.subString(5) /MOV /R:2 /w:300 /NFL /NDL
                        }
                    }
                }
                # Now rename file (remove Film_ ) then
                Rename-Item $StartLocation$TSFile $TSfile.Name.subString(5)
                ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $FilmLocation.subString(0,$FilmLocation.Length-1) $TSfile.Name.subString(5) /MOV /R:2 /w:300 /NFL /NDL
            }
            else
            {
                # Check the XML file first to get actual name, then check against already stored TV Series
                # Logic = If series exists move to Otherlocation (assuming that is your area for getting TV episodes moved)
                # If not a series that exists leave along - simply setup the folder in your TV series folder to get it to be moved
                # If it has no episode number then it will NEVER be moved
                 
                $tmp = $TSfile.Name.IndexOf(".ts")
                $tmp1 = $TSfile.Name.SubString(0,$tmp)
                # Section Used if not Film
                $AllFile = $Alllist | Where-Object {$_.Name -like "*$tmp1*"}
                
                    if (!$Allfile)
                    {
                    }
                    else
                    {
                        [xml]$xmlFile = get-content $StartLocation$Allfile
                        $xmlTitleraw = $xmlFile.tags.tag.SimpleTag  | Where-Object {$_.Name -eq "TITLE"}
                        $xmlSeries = $xmlFile.tags.tag.SimpleTag  |Where-Object {$_.Name -eq "SERIESNUM"}
                        $xmlEpisode = $xmlFile.tags.tag.SimpleTag  | Where-Object {$_.Name -eq "EPISODENUM"}
                        # Test for (R)
                        $tmpValue = $xmlTitleraw.value.indexof("(R)")
                        if ($tmpValue -eq -1) 
                        {
                            $tmpValue = $xmlTitleraw.value.length
                        }
                        $xmlTitle = $xmlTitleraw.value.substring(0,$tmpValue).trim()
                        
                        # if Seriesnum -eq missing then ignore all files
                        if ($xmlSeries.value -eq '') 
                        {
                            #Do Nothing    
                        }
                        else
                        {
                        
                            # Rename and Move to the other Location
                            # Rename first
                            $tmpValue = $xmlEpisode.value.indexof("/")
                            if ($tmpValue -eq -1)
                            {
                                $tmpValue = $xmlEpisode.value.length
                            }
                            $xmlEpisodeNum = $xmlEpisode.value.substring(0,$tmpValue)
                            $newName = $xmlTitle + " S" + $xmlSeries.value + "E" + $xmlEpisodeNum
                            $newNameXML = $newName + ".XML"
                            $newNameTS = $newName + ".ts"
                            
                            # Now need to check if the series exists in the TVShowlist
                            
                            $TVShowTest = $TVShowlist | Where-Object {$_.Name -like "$xmlTitle*"}
                            if (!$TVShowTest -and $TVShowList -ne "")
                            {
                            }
                            else
                            {
                                # If it appears in the list then this will be run
                                if ($DeleteOther -eq 1)
                                {
                                    # Delete the file
                                    Remove-Item $StartLocation$Allfile
                                }
                                else
                                {                                
                                    Rename-Item $StartLocation$AllFile $newNameXML
                                    ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $OtherLocation.subString(0,$OtherLocation.Length-1) $newNameXML /MOV /R:2 /w:300 /NFL /NDL
                                }
                                      
                                # Move to the OtherLocation
                                Rename-Item $StartLocation$TSfile $newNameTS
                                ROBOCOPY $StartLocation.subString(0,$StartLocation.Length-1) $OtherLocation.subString(0,$OtherLocation.Length-1) $newNameTS /MOV /R:2 /w:300 /NFL /NDL
                            }
                        }
                    }
                
            }
        }
    }
 

Users who are viewing this thread

Top Bottom