Skip to content

Instantly share code, notes, and snippets.

@scrthq
Created March 27, 2018 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scrthq/2a2ecaab1e9c7c3af4049205d439bf03 to your computer and use it in GitHub Desktop.
Save scrthq/2a2ecaab1e9c7c3af4049205d439bf03 to your computer and use it in GitHub Desktop.
Quick function to rename subtitle files for Plex subtitle cleanup jobs to remove duplicate locale's. Assumes you are only using a single locale for subs. Defaults to English and SRT extension.
function Rename-Subtitles {
[CmdletBinding()]
Param
(
[parameter(Mandatory = $true,Position = 0,ValueFromPipeline = $true)]
[ValidateScript({Test-Path $_})]
[String]
$Path,
[parameter(Mandatory = $false)]
[String]
$Locale = "en",
[parameter(Mandatory = $false)]
[String]
$Extension = "srt"
)
Process {
foreach ($P in $Path) {
$subs = Get-ChildItem $P -Recurse | Where-Object {$_.Name -like "*.$($Extension)"}
foreach ($sub in $subs) {
$clearName = ((($sub.Name -replace "\.$($Locale)\.",".") -replace "\.$($Locale)\.",".") -replace "\.$($Locale)\.",".") -replace "\.$($Extension)$",".$($Locale).$($Extension)"
Write-Host -ForegroundColor Yellow "Renaming '$($sub.FullName)' to '$($sub.Directory)\$($clearName)'"
Rename-Item -Path $sub.FullName -NewName $clearName -Force
}
}
}
}
@scrthq
Copy link
Author

scrthq commented Mar 27, 2018

Tip Time

Place this in your PowerShell $PROFILE for ease of use by doing the following:

  1. Open your PowerShell profile by running ise $profile. If you get an error that the file doesn't exist, run "" > $profile to create it then re-run ise $profile
  2. Copy the function in the Gist and paste it in your profile once it opens in PowerShell ISE, then save.
  3. Close PowerShell ISE and your first PowerShell window.

Once you do the above, the function will be available in session whenever you open PowerShell.

Usage

Run the following, replacing the fake path "C:\Media Library" with the actual path to the folder containing your media:

Rename-Subtitles -Path "C:\Media Library"

You should see output similar to the following showing you all the files it's renaming:

Renaming 'E:\SubsTest\noEn.srt' to 'E:\SubsTest\noEn.en.srt'
Renaming 'E:\SubsTest\oneEn.en.srt' to 'E:\SubsTest\oneEn.en.srt'
Renaming 'E:\SubsTest\threeEn.en.en.en.srt' to 'E:\SubsTest\threeEn.en.srt'
Renaming 'E:\SubsTest\twoEn.en.en.srt' to 'E:\SubsTest\twoEn.en.srt'
Renaming 'E:\SubsTest\subfolder\noEn.srt' to 'E:\SubsTest\subfolder\noEn.en.srt'
Renaming 'E:\SubsTest\subfolder\oneEn.en.srt' to 'E:\SubsTest\subfolder\oneEn.en.srt'
Renaming 'E:\SubsTest\subfolder\threeEn.en.en.en.srt' to 'E:\SubsTest\subfolder\threeEn.en.srt'
Renaming 'E:\SubsTest\subfolder\twoEn.en.en.srt' to 'E:\SubsTest\subfolder\twoEn.en.srt'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment