I need emergency help. I attempted to automate renaming files using one of the solutions in this post: Loop through folders/files and replace word
But upon using the example, a large number of the files were mp3 files and it appears they no longer function to play the audio they once did. After a number of trials and errors, nothing was seeming to change these filenames. I just wanted to remove a certain phrase from all the filenames. After this one attempt, I could see that something was happening to the files as I could see Windows Explorer refreshing the files one by one, but never could get the filename to change, but I killed the script when I saw that for some reason the filesizes were changing. Lo and behold, the files were no longer playing audio. So I'm wondering what actually happened to these files and if there is any hope to recover them. This is the script (with directories changed):
$matchString = 'Final'
$replaceString = ''
$files = Get-ChildItem -Path 'M:\Music\SomeProject' -filter *.mp3 -File
foreach ($file in $files) {
$matchFound = $false
$output = switch -regex -file $file.fullname {
$matchString { $_ -creplace $matchString,''
$matchFound = $true
}
default { $_ }
}
if ($matchFound) {
$output | Set-Content $file.fullname
}
}
I suspect the problem occurred due to my no longer referencing $replaceString and instead hardcoding '' in its place, in any desperate attempt to get any part of the filename to change. Any ideas as to how this happened and if there is any chance of recovery?
Unfortunately this looks like you have written garbage in the affected MP3 files. Your only option is to restore them from another a source (backup, download, rip from CD.)
Where did it go wrong? In the Set-Content
part. The cmdlet will write data into a file, whilst you wanted merely to rename the file. The right cmdlet for renaming is Rename-Item
.
As for future attempts, try and use the -WhatIf
switch. It is supported by several cmdlets and will tell you what the command is about to do, but not actually do it yet. Examine its output and remove the switch only when you are sure the output looks sensible.