I have some files like this
Hooligans (BDrip (1080).mkv
Hocus Pocus [DIVX - ITA] - Walt Disney - HQ-.avi
Ho Cercato Il Tuo Nome h265.avi
Ho Cercato Il Tuo Nome 720.rar
Halloween 2 - Il Signore Della Morte.avi
Highlander 2 1080p.mkv
Heartbreakers - Vizio di famiglia.avi
Il cavaliere oscuro.mkv
Il cavaliere oscuro - il ritorno 1080p.mkv
In the same directory I have a text file (movielist.txt) that is formatted always in this way: name + year.
Year is always from 19**-20** and is inside brackets
Hooligans (1995)
Hocus Pocus (1993)
Ho cercato il tuo nome (2012)
Halloween 2 – Il signore della morte (1981)
Highlander 2 – Il ritorno (1990)
Heartbreakers – Vizio di famiglia (2001)
Il cavaliere oscuro (2008)
Il cavaliere oscuro - Il ritorno (2012)
... other text
I try to rename files so:
Hooligans (1995).mkv
Hocus Pocus (1993).avi
Ho cercato il tuo nome (2012).avi
Ho cercato il tuo nome (2012).rar
Halloween 2 – Il signore della morte (1981).avi
Highlander 2 – Il ritorno (1990).mkv
Heartbreakers – Vizio di famiglia (2001).avi
Il cavaliere oscuro (2008).mkv
Il cavaliere oscuro - il ritorno (2012).mkv
Note: the number of files to be renamed is not the same as the number of lines in the text file. In the text file there can be 100 lines while the number of files to rename could be 50.
There are two ways to do but are insufficient to perform that I request
1st
$movieList = Get-Content movielist.txt
# enumerate all movies
:outer foreach($file in Get-ChildItem "C:\Users\Peter\Desktop\test skript\test2" -File) {
$bn = $file.BaseName
# enumerate all lines of the movies to rename file
foreach($movie in $movieList) {
# if the Base Name is contained in the movie to rename, ie:
# 'Young Devils (1999)' -like '*young Devils*'
if($movie -like "*$bn*") {
# rename the file using the movie + the file's extension, ie:
# Young Devils (1999) + .mkv
Rename-Item -LiteralPath $file.FullName -NewName ($movie + $file.Extension)
# no need to keep comparing this file, if we're here
# we can continue with next file
continue outer
}
}
}
2nd:
$rootFolder = "C:\Users\Peter\Desktop\test skript\test1"
$files = Get-ChildItem -Path $rootFolder -File
$filesNames = Get-Content -Path "$rootFolder\movielist.txt"
foreach ($name in $filesNames)
{
$files | Where-Object -FilterScript {
$name -match [Regex]::Escape($_.BaseName)
} | ForEach-Object -Process {
$ext = $_.Extension
Rename-Item -LiteralPath $_.FullName -NewName "$name$ext" -EA 0
}
}
Any idea on how to rename the files of my example?