Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with a media renaming script

Nagorb83

Programmer
Apr 16, 2025
1
Hello all, hope everyone is doing great. I am wondering if I can help me with a powershell script I am currently working on (I am newish to powershell). The script that in question is for re-naming media files. This is what I
have so far (please see below). The problem I am having is when there are multiple media files (please see structure and file examples below) I end up with an error (see below) about file already exist and then it overwrites other media files with the same media name.
Any help that could be offered would be greatly appreciated!!

=================
Structure/files |
=================

Completed (dir)
|
|---> Media file 1 (2000) [1080p] [BluRay] [5.1] [YTS.MX] - (dir)
| |---> Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].mp4 -(file)
| |---> Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].srt -(file)
|
|----->Media file 2 (2000) [720p] [WEBRip] [YTS.MX] - (dir)
|---> Media.File.2.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4 -(file)


=================
Error I recieve |
=================

Rename-Item : Cannot create a file when that file already exists.
At C:\Users\User\Desktop\Media rename.ps1:53 char:72
+ ... ere {$_.name -match $media.extension} | Rename-Item -NewName $newName
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (C:\Torrents\Ren...AC-[YTS.MX].mp4:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand


===============================
Structure after script is ran |
===============================
Renamed Movies
|---> Media file 1 (2000).mp4 -(file)
|---> Media file 1 (2000).srt -(file)
|---> Media.File.1.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4 (this was suppose to be Media File 2.mp4)




===============
Script |
===============

# ***********
# Variables *
# ***********

$newLocation = "C:\Torrents\Renamed Movies"
$path = "C:\Torrents\Completed"

# *****************************************************************************************
# Locate the media files in the specified folder (this folder will never change location) *
# *****************************************************************************************

$mediaFile = Get-ChildItem -LiteralPath "$path" -Recurse | Where-Object {$_.extension -match ".srt" -or $_.extension -match ".mkv" -or $_.extension -match ".mp4"}

foreach ($media in $mediaFile) {

# ************************************************************************************************
# Remove any periods from the file name and clean up any extra junk at the end of the file name *
# ************************************************************************************************

$fullFileName = $media.baseName.replace("."," ").replace("(","")

$cleanName = $fullFileName -replace '\s*\d.*'

# *******************************************
# Find the four digit year in the file name *
# *******************************************

$year = ([Regex]::Matches($fullFileName, '(19|20)[\d]{2,2}')).value

# **********************************************************************************************
# Generate a new file name following the standard format of [Movie Name] ([Year]).[extension] *
# **********************************************************************************************

$newName = "$cleanName ($year)$($media.extension)"

# *******************************
# Change into the new directory *
# *******************************

Set-Location -LiteralPath $newLocation

# ******************************************
# Move the movie file into the new folder *
# ******************************************

Move-Item -LiteralPath $media.fullName -Destination $newLocation

# ******************************
# Rename the newly moved file *
# ******************************

Get-ChildItem $newLocation | where {$_.name -match $media.extension} | Rename-Item -NewName $newName

}

pause
 
|---> Media.File.1.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4 (this was suppose to be Media File 2.mp4)
Are you sure it was |---> Media.File.2.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4 before your script ran? As it failed to rename the 720p file that points out it was Media.File.1.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4 already before the script ran. And that would rename to Media file 1 (2000).mp4 which obviously already exists from renaming Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].mp4

I suggest you also keep the resolution parts of the name, i.e. 1080p and 720p.
 

Part and Inventory Search

Sponsor

Back
Top