powershellscriptingrenametext-manipulationfile-manipulation

PowerShell - Why is this not renaming my files?


I have been looking around for a way to quickly and easily rename hundreds os files in one go. something where I only have to change smalle parts for it to be reused somewhere else. So i ended up starting to make this script. shown below...

the output should come out like this:

Show Title - SXX.EXXX - Episode title - [release year]

the raw files all looks like this:

XXX Episode title [release year]

It does not work right now. and i haven't been able to see why yet.

Whenever i run it, it does nothing. but i do not get any error message.

$ShowTitle = "My Title -"
$SeasonNumber = "02" 

# Getting all child files (In ALL subfolders)
$files = Get-Childitem –Path Get-Location -Recurse | 
Where-Object { $_.Name -match $_.Name } | 

# Insert a ' - ' between the episode number and the episode text.
Rename-Item -NewName {$_.BaseName.insert(5,'-') + $_.Extension} |

# Append title and season number to the beginning of the file. 
Rename-Item -NewName  { $ShowTitle + "S" + $SeasonNumber + ".E" + $_.Name} | 

# Makes a "-" between episode title and year of release.
Rename-Item -NewName { $_.Name -replace '\[', '- [' }

it worked on a smaller scale before. like this:

$files = Get-Childitem –Path "C:\Users\user\Videos\Series\show\Season x" -Recurse |
Where-Object { $_.Name -match 'show title' } | 
Rename-Item -NewName { $_.Name -replace '\[', '- [' }

But i would like to do all the steps above in one go.

Can someone give me a hint so I can find the right answer to my little problem? Thank you in advance.


Solution

  • You've got a lot of bugs here.

    Get-Childitem –Path Get-Location -Recurse
    

    This doesn't make sense. You're looking for a file or folder in the current directory with the literal name Get-Location. Like C:\Get-Location\. If you want to get the files in the current directory, you just don't specify the -Path parameter: Get-ChildItem -Recurse.

    Where-Object { $_.Name -match $_.Name } is kind of nonsense code? The right hand side of the -match operator is going to be treated as a regular expression. That means . means "any character", square brackets and parentheses have special meaning, and so on. It's often going to always be true, but I can't imagine that you actually want to do what that says. It's very possible to construct a valid filename that doesn't match a regular expression with the same string value. For example '[01] File.avi' -match '[01] File.avi' is false.

    Second, the -NewName parameter takes a string, while {$_.BaseName.insert(5,'-') + $_.Extension} is a ScriptBlock. That may work because some parts of Powershell allow that, but idiomatically I would say that it's wrong because it will not work consistently. A better option would be to use a string with embedded subexpressions like -NewName "$($_.BaseName.Insert(5,'-'))$($_.Extension)"

    Finally, Rename-Item doesn't pass any output to the pipeline without the -PassThru parameter. You'd only process the first item and then I imagine the system would complain of an empty pipeline or only the first Rename-Item would do anything.

    Try something like this:

    $ShowTitle = "My Title -"
    $SeasonNumber = "02" 
    
    # Getting all child files (In ALL subfolders)
    $files = Get-Childitem -Recurse | 
    Where-Object { $_.Name -match 'some value or delete this command if you want all files' } | 
    
    # Insert a ' - ' between the episode number and the episode text.
    Rename-Item -NewName "$($_.BaseName.Insert(5,'-'))$($_.Extension)" -PassThru |
    
    # Append title and season number to the beginning of the file. 
    Rename-Item -NewName "$($ShowTitle)S$($SeasonNumber).E$($_.Name)" -PassThru | 
    
    # Makes a "-" between episode title and year of release.
    Rename-Item -NewName "$($_.Name -replace '\[', '- [')" -PassThru