I have written a powershell script that will scan a directory for any *mkv
, *.mp4
, *.avi
files, and will parse the filename to get Name, Season Number, Episode number. Then will check against destination directory to determine
If Name exists (if not create it)
If season number exists (if not create it)
Once both these checks have been performed move the file to the destination dir
This syntax works for my purpose, but it is slow... I am lower than a beginner when it comes to powershell (is there such a thing?) - And would like any helpful hints or code mods that the more experienced programmers can provide.
$fileDirectory = "C:\Test\"
foreach ($file in Get-ChildItem $fileDirectory | where {($_.extension -eq '.mkv') -or ($_.extension -eq '.avi') -or ($_.extension -eq '.mp4')}){
#Setting parent dir to check
$ParentDir = "\\C:\TV Shows\"
#setting param to split
$parts =$file.Name -split '\.'
#capturing variables
$ShowName = $parts[0].Trim()
$SeasonNumber = $parts[1].substring(0,3)
$EpisodeNumber = $parts[1].substring(3,3)
#Converting numeric season value to text
switch ($SeasonNumber){
S01
{
$SeasonNumber = "Season 01";
break
}
S02
{
$SeasonNumber = "Season 02"
break
}
S03
{
$SeasonNumber = "Season 03"
break
}
S04
{
$SeasonNumber = "Season 04"
break
}
S05
{
$SeasonNumber = "Season 05"
break
}
S06
{
$SeasonNumber = "Season 06"
break
}
S07
{
$SeasonNumber = "Season 07"
break
}
S08
{
$SeasonNumber = "Season 08"
break
}
S09
{
$SeasonNumber = "Season 09"
break
}
S10
{
$SeasonNumber = "Season 10"
break
}
default {"Something else happened"; break}
}
#Setting path variables
$ShowDir = Join-Path -Path $ParentDir -ChildPath $ShowName
$SeasonDir = Join-Path -Path $ShowDir -ChildPath $SeasonNumber
#Write-Host "$ShowDir\"
#Write-Host $SeasonDir
#Checking if Show already has a folder
if (!(Test-Path "$ShowDir\" -PathType Container)) {
New-Item -ItemType Directory -Force -Path "$ShowDir\"
}
#Checking if directory already has folder
If(!(Test-Path "$SeasonDir\" -PathType Container))
{
#if it does not create it
New-Item -ItemType Directory -Force -Path $SeasonDir
}
Move-Item -Path $file.FullName -Destination $SeasonDir
}