While working on my media server, see more about that here, I came across a repetitive task where code could help. The issue: as you move content to your media server it gets repetitive with any TV show. Jellyfin likes a certain format to ingest the movie files. As shown here, it is best to have an overall folder with the name of the show, a sub folder with the season number and then the episode. Renaming every file with the season number and episode name is repetitive. Why not use a PowerShell script with robocopy?

The Solution

Using PowerShell, I loop through the contents of a folder to rename the files. This assumes the files are already listed in the correct order for the episodes of the season. Then I use robocopy to move them to my NAS (network attached storage). Finally, I remove the old folder and contents that are not needed.

The PowerShell script with robocopy

First is some easy variable declaration with some built in logic to account for some user error. Take note: the “-LiteralPath” switch is important and a way to account for Windows style variations in folder structures.

# Variable Declaration
$season = Read-Host "Enter the season number"
$season = $season -replace "[^0-9]"
$directory = Read-Host "Enter the directory to work in"
$NumOfItems = (Get-ChildItem -LiteralPath $directory | Measure-Object).Count 
$TempNum = $NumOfItems
$DocFiles = (Get-ChildItem -LiteralPath $directory | ForEach-Object {$_.Name})
$ToDirectory = Read-Host "Enter the directory to copy to (Creates Season folder for you)"

Next create the folder you need in the destination directory.

# Creates the Season Folder for you. 
$TempDirectory = $ToDirectory + '\Season ' + $season
$ToDirectory = $TempDirectory
New-Item -Path $ToDirectory -ItemType Directory

Following that, the loop uses logic to rename the files appropriately.

# Loop renames all the files in the directory specified. Name is a combination of the variables.
foreach ($file in $DocFiles){
    $TempNum--
    $Rename = "S"+$season+" E"+($NumOfItems - $TempNum)
    # As long as the video file is a three character extension, this will rename each file appropriately.
    $file | Rename-Item -LiteralPath $directory"\$file" -NewName {$Rename + $file.Substring($file.Length - 4)} -Verbose
    Clear-Variable Rename
}

Lastly, move the files to the destination and clear out the source files. If you want to know more about robocopy you can find it on Microsoft’s site here. It’s not PowerShell, but can be used in your script.

# Move the files from point A to point B. 
robocopy $directory $ToDirectory  /E /Z /R:5 /W:5 /TBD /V /MT:16

# Removal of the from directory and contents after RoboCopy is done.
Remove-Item -LiteralPath $directory -Force -recurse

Here is the entire script:

# Author:     Powersjo
# Start Date: 2/06/2023
# Version:    1.7

# Variable Declaration
$season = Read-Host "Enter the season number"
$season = $season -replace "[^0-9]"
$directory = Read-Host "Enter the directory to work in"
$NumOfItems = (Get-ChildItem -LiteralPath $directory | Measure-Object).Count 
$TempNum = $NumOfItems
$DocFiles = (Get-ChildItem -LiteralPath $directory | ForEach-Object {$_.Name})
$ToDirectory = Read-Host "Enter the directory to copy to (Creates Season folder for you)"

# Creates the Season Folder for you. 
$TempDirectory = $ToDirectory + '\Season ' + $season
$ToDirectory = $TempDirectory
New-Item -Path $ToDirectory -ItemType Directory

# Loop renames all the files in the directory specified. Name is a combination of the variables.
foreach ($file in $DocFiles){
    $TempNum--
    $Rename = "S"+$season+" E"+($NumOfItems - $TempNum)
    # As long as the video file is a three character extension, this will rename each file appropriately.
    $file | Rename-Item -LiteralPath $directory"\$file" -NewName {$Rename + $file.Substring($file.Length - 4)} -Verbose
    Clear-Variable Rename
}

# Move the files from point A to point B. 
robocopy $directory $ToDirectory  /E /Z /R:5 /W:5 /TBD /V /MT:16

# Removal of the from directory and contents after RoboCopy is done.
Remove-Item -LiteralPath $directory -Force -recurse

Hopefully you found this helpful for your PowerShell script with robocopy, good luck.

Check out my previous blog post here.

Enter your email address to subscribe to this blog and receive notifications of new posts!

If you enjoy this blog check out my affiliate links here to support this site: https://powersjo.com/powersjo-affiliate-links/

God bless you!

Published by Powersjo

If you want to contact me I can be found here: https://gab.com/Powersjo Christ is King