Back in 2021 the directory structure of a hard drive that contained downloaded video packages got corrupted.
I used a deep recovery program that got all the individual files back plus multiple copies of old deleted files but the folders were gone and the file names were renamed to a generic filename. I ended up re-downloading everything I could instead.
But now I need files that are no longer on the web but might be in that archive. I spent yesterday and this morning looking at various code to "rename the file to the Title" and here is what I worked out removing all unnecessary operations.
I added error checking to skip files that have a $null Title or if it was already renamed to the Title. Illegal characters in a filename are replaced with underscores '_'. I added a counter in case there are multiple files with the same Title and append that number in the filename.
It took me a bit to understand what wasn't actually needed in the example code I was learning from. The $PrevT was for my debugging only so I could see it progress through the file list.
$file = ""
$Title = ""
$blah = 0
#$PrevT = ""
#$illegalchars2 = [string]::join('',([System.IO.Path]::GetInvalidFileNameChars())) -replace '\\','\\'
$illegalchars2 = '"<>|:*?\\/'
$LocalDir = 'K:\recup_folders\recup_dir.1\temp\'
$FType = '.mp4'
$AllFiles = Get-ChildItem $LocalDir -Filter *.mp4
$ShellApplication = New-Object -ComObject Shell.Application
foreach ($SingleFile in $AllFiles)
{
$blah = $blah + 1
$folder = ShellApplication.Namespace($SingleFile.Directory.FullName)
$file = $folder.ParseName($SingleFile.Name)
$Title = $file.ExtendedProperty('System.Title')
#$FixT = ([char[]]$Title | where { [IO.Path]::GetinvalidFileNameChars() -notcontains $_ }) -join ''
$FixT = $Title
$FixedT = $FixT -replace "[$illegalchars2]",'_'
if ($null -ne $Title)
{
$OldName = "$LocalDir$Singlefile"
$NewName = "$LocalDir$FixedT$FType"
'-----------------'
#"Previous: $PrevT"
"Current File: $Singlefile"
"Unfixed Title: $Title"
"FixedT: $FixedT"
"OldName: $OldName"
if ($OldName -eq $NewName)
{ 'Skipping file already fixed.' }
else
{
if ((Test-Path -Path $NewName -PathType Leaf))
{
"Repeat File detected: $NewName"
$NewName = "$LocalDir$FixedT($blah)$FType"
}
"Final New Name: $NewName"
Rename-Item $OldName -NewName $NewName
'----Done---------
'
}
#$PrevT = $Title
}
else { "Skipping blank Title file: $Singlefile" }
}
Sample output:
-----------------
Current File: 00PM - Four Points by Sheraton Orlando ✅.mp4
Unfixed Title: Flat Earth Meetup Florida - April 14 - 7:00PM - Four Points by Sheraton Orlando ✅
FixedT: Flat Earth Meetup Florida - April 14 - 7_00PM - Four Points by Sheraton Orlando ✅
OldName: K:\recup_folders\recup_dir.1\temp\00PM - Four Points by Sheraton Orlando ✅.mp4
Final New Name: K:\recup_folders\recup_dir.1\temp\Flat Earth Meetup Florida - April 14 - 7_00PM - Four Points by Sheraton Orlando ✅.mp4
----Done---------
-----------------
Current File: Black Death talks Flat Earth on Heavy Metal Relics ✅.mp4
Unfixed Title: Black Death talks Flat Earth on Heavy Metal Relics ✅
FixedT: Black Death talks Flat Earth on Heavy Metal Relics ✅
OldName: K:\recup_folders\recup_dir.1\temp\Black Death talks Flat Earth on Heavy Metal Relics ✅.mp4
Skipping file already fixed.
-----------------
Current File: f11860480.mp4
Unfixed Title: Flat Earth Man "Welcome to the Satellite Hoax" ✅
FixedT: Flat Earth Man _Welcome to the Satellite Hoax_ ✅
OldName: K:\recup_folders\recup_dir.1\temp\f11860480.mp4
Final New Name: K:\recup_folders\recup_dir.1\temp\Flat Earth Man _Welcome to the Satellite Hoax_ ✅.mp4
----Done---------
-----------------
Current File: f11981680.mp4
Unfixed Title: Flat Earth Man "Welcome to the Satellite Hoax" ✅
FixedT: Flat Earth Man _Welcome to the Satellite Hoax_ ✅
OldName: K:\recup1\recup_folders\recup_dir.1\temp\f11981680.mp4
Repeat File detected: K:\recup_folders\recup_dir.1\temp\Flat Earth Man _Welcome to the Satellite Hoax_ ✅.mp4
Final New Name: K:\recup_folders\recup_dir.1\temp\Flat Earth Man _Welcome to the Satellite Hoax_ ✅(4).mp4
----Done---------
-----------------
Current File: f13881504.mp4
Unfixed Title: Example: <of> /bad\ *bad?
FixedT: Example_ _of_ _bad_ _bad_
OldName: K:\recup_folders\recup_dir.1\temp\f13881504.mp4
Final New Name: K:\recup_folders\recup_dir.1\temp\Example_ _of_ _bad_ _bad_.mp4
----Done---------
Skipping blank Title file: f18489568.mp4
-----------------
Current File: Rock and Roll Flat Earth Song Music - The Zetetic Astronomers by Neo Retros - Mark Sargent ✅.mp4
Unfixed Title: Rock and Roll Flat Earth Song / Music - The Zetetic Astronomers by Neo Retros - Mark Sargent ✅
FixedT: Rock and Roll Flat Earth Song _ Music - The Zetetic Astronomers by Neo Retros - Mark Sargent ✅
OldName: K:\recup_folders\recup_dir.1\temp\Rock and Roll Flat Earth Song Music - The Zetetic Astronomers by Neo Retros - Mark Sargent ✅.mp4
Final New Name: K:\recup_folders\recup_dir.1\temp\Rock and Roll Flat Earth Song _ Music - The Zetetic Astronomers by Neo Retros - Mark Sargent ✅.mp4
----Done---------
Posts I used to figure this out by taking a key piece of each: