infamousjoeg
11/7/2016 - 2:06 AM

PowerShell script created to finish the unpack process started by SickRage, but normally failing -- this simplifies the mass moving to their

PowerShell script created to finish the unpack process started by SickRage, but normally failing -- this simplifies the mass moving to their final homes.

# VARIABLES
$i = 0
$x = 0
$dirList = Get-ChildItem -Directory
$seasonArr = @{"00" = "Season 00";"01" = "Season 01";"02" = "Season 02";"03" = "Season 03";`
                "04" = "Season 04";"05" = "Season 05";"06" = "Season 06";"07" = "Season 07";`
                "08" = "Season 08";"09" = "Season 09"}

# EXECUTION
foreach ($dir in $dirList) {

    $i = $i + 1
    $properName = $dir.Name
    $readDir = Get-ChildItem -Path $dirName -Recurse

    if ($properName -match "_UNPACK_") {

        $remUnpack = $properName.Substring(8)               # Removing leading _UNPACK_
        $pos = $remUnpack.IndexOf("0")                      # Setting center point (First 0 encountered)
        $showName = $remUnpack.Substring(0, $pos)           # Left part of file name from $pos
        $showName = $remUnpack.Substring(0, $pos-2)         # Trim off white space and S 
        $season = "S0" + $remUnpack.Substring($pos+1, 4)    # Grabbing SxxExx
        Write-Host "$i. $showName - $season"                # Confirming proper values

        foreach ($file in $readDir) {

            $x = $x + 1                                     # Counter
            $seasonPos = $season.IndexOf("E")               # Setting center point (First E encountered)
            $seasonVar = $season.Substring(0, $seasonPos)   # Left part of file name from $seasonPos
            $seasonVar = $seasonVar.TrimStart("S")          # Trimming leading S for just the season number
            $seasonName = $seasonArr.$seasonVar             # Pulling proper season name from hash table made earlier
            $source = "\\freenas\Downloads\Complete\$properName"
            $destination = "\\freenas\TV\$showName\$seasonName"
            $fileName = $file.Name

            try {
                robocopy $source $destination $fileName /MOV    # Requires robocopy - only moves files from source to destination
            } catch {
                Write-Host "[ ERROR ] $showName - $season : Error During Move"
            }

            Write-Host "[ $x / $i ] $showName - $season : Moved Successfully"
        }
    }
}