Howdy folks,

If you’re like me and have ever digitized a bunch of media (in this specific case, the first couple seasons of It’s Always Sunny in Philadelphia from DVDs) you’ve probably run across problems getting applications such as Plex to properly classify episodes and end up with something that looks like this:

Despite totally reasonable file names like “Its Always Sunny in Philadelphia Season 03 Episode 02 “, PLEX is not able to correctly parse the names out. To fix this, we are going to leverage the Powershell CMDlets Get-ChildItem and pipe that to Rename-Item. Easy, right?

Let’s use the following as a base to edit, replacing the “old” phrase with the “new” one:

Get-ChildItem -filter "*old*" -recurse | Rename-Item -NewName {$_.name -replace 'old','new'}

That little snippet should crawl through the current directory (and those under it) and rename the old phrase to the new. That “|” character in there takes the output of the first half and pipes it to the other half…

  • Get-ChildItem -filter “*old*”
    • This grabs files with the “old” anywhere in the filename – even if it occurs multiple times.
  • -recurse
    • This actually allows the cmdlet to dig into child folders – super handy if you wanted to, say, knock out an entire series rather than going one folder at a time. Depending on your show structure you may not be able to do this, which is why I left it out later.
  • Rename-Item -NewName
    • Renames an item
    • Note: you can throw on a -whatif parameter to get an output of files that would be changed
  • {$_.name -replace ‘old’,’new’}
    • This tells Rename-Item to rename the string “old” with “new”

Using this knowledge, we can transform this script into something like:

Get-ChildItem -Filter '*Season 03 Episode *' | Rename-Item -NewName {$_.name -replace 'Season 03 Episode ','s03e'}

In this example, we are only selecting “Season 03 Episode” to retain the 03. This allows us to change our naming scheme to s03e03!

Using -whatif we are able to see our effects without breaking anything:

PS Microsoft.PowerShell.Core\FileSystem::\\nfs-01\Media SMB Share\tv\It's Always Sunny in Philadelphia\Season 3> Get-ChildItem
-Filter '*Season 03 Episode *' | Rename-Item -NewName {$_.name -replace 'Season 03 Episode ','s03e'} -whatif
What if: Performing the operation "Rename File" on target "Item: \\nfs-01\Media SMB Share\tv\It's Always Sunny in Philadelphia\Season 3\Its Always Sunny in Philadelphia Season 03 Episode 04 - The Gang Gets Held Hostage.avi Destination: \\nfs-01\Media SMB Share\tv\It's Always Sunny in Philadelphia\Season 3\Its Always Sunny in Philadelphia s03e04 - The Gang Gets Held Hostage.avi".

 

That’s all! A pretty simple command that’s exceptionally flexible at renaming things from production data to your favorite TV show.


Leave a Reply

Your email address will not be published. Required fields are marked *