Help with a media renaming script

Nagorb 20 Reputation points
2025-04-15T23:30:18.58+00:00

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

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,915 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 35,726 Reputation points
    2025-04-16T17:45:38.1633333+00:00

    You really need to use this forum's Code Block tool when posting Powershell code. Otherwise, certain characters get treated as formatting characters. That's why your post looks strange.

    I tried to recreate your folder/file structure with these command prompt statements. I don't know if this is correct or not.

    cd \temp
    rd /q /s RenameTest
    md RenameTest
    cd RenameTest
    md Input
    md Output
    cd Input 
    md "Media file 1 (2000) [1080p] [BluRay] [5.1] [YTS.MX]"
    cd "Media file 1 (2000) [1080p] [BluRay] [5.1] [YTS.MX]"
    echo "xxxx" > "Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].mp4"
    echo "xxxx" > "Media.File.1.2000.1080p.BluRay.x264.AAC5.1-[YTS.MX].srt"
    cd ..
    md "Media file 2 (2000) [720p] [WEBRip] [YTS.MX]"
    cd "Media file 2 (2000) [720p] [WEBRip] [YTS.MX]"
    echo "xxxx" > "Media.File.2.2000.720p.WEBRip.x264.AAC-[YTS.MX].mp4"
    cd \temp\renametest
    dir /b /s      
    
    
    

    I then tweaked your script to get it to work. One problem appears to be that the cleanName variable is not unique enough when all files are getting stored in the same destination folder. I added code to look for an existing file and then add a counter to the name to make it unique.

    I don't know if that's what you want or not.

    #***********
    #Variables *
    #***********
    $newLocation = "C:\Temp\RenameTest\Output"
    $path = "C:\Temp\RenameTest\Input"
    #*****************************************************************************************
    #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) {
        "---------------------------------------------------"
        "Media.fullname = {0}" -f $media.fullname
        ""
        #************************************************************************************************
        #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("(","")
        "fullFileName = {0}" -f $fullFileName
        $cleanName = $fullFileName -replace '\s*\d.*'
        "cleanName = {0}" -f $cleanName
        #*******************************************
        #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)"
        "newName = {0}" -f $newName
        $newNamePlusDir = "$newLocation\$newname" 
        "newNamePlusDir = {0}" -f $newNamePlusDir
        $movedFileName = "{0}\{1}" -f $newLocation, $media.Name
        "Moved name = {0}" -f $movedFileName
         
      
        # Look to see if a file with the new name already exists in the destination folder
        $count = 1
        while (Test-Path $newNamePlusDir) {
            "A file with the newname already exists in the output folder. "
            $newName = "$cleanName ($year)($count)$($media.extension)"
            "newName = {0}" -f $newName
            $newNamePlusDir = "$newLocation\$newname" 
            "newNamePlusDir = {0}" -f $newNamePlusDir
            $count++
        }
       
       
        #******************************************
        # Move the movie file into the new folder *
        #******************************************
        Move-Item -LiteralPath $media.fullName -Destination $newLocation 
    
        #******************************************
        # Now rename it                           *
        #******************************************   
        Rename-Item -LiteralPath $movedFileName -NewName $newName  
        ""
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.