Sunday, January 20, 2013

Powershell in Real Life: Fixing an MP3 Album I downloaded

Powershell has some quirks, but all in all is awesome. Here's an example:

Today I downloaded an album from the internet...

PS C:\Users\idvor_000\Downloads\music> dir
    Directory: C:\Users\idvor_000\Downloads\music
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/20/2013  11:47 AM    8648447 01-Loreena_McKennitt-The_Book_Of_Secrets-Prologue.mp3
-a---         1/20/2013  11:48 AM   12194913 02-Loreena_McKennitt-The_Book_Of_Secrets-The_Mummers_Dance.mp3
-a---         1/20/2013  11:47 AM   12026448 03-Loreena_McKennitt-The_Book_Of_Secrets-Skellig.mp3
-a---         1/20/2013  11:49 AM   20341141 05-Loreena_McKennitt-The_Book_Of_Secrets-The_Highwayman.mp3
-a---         1/20/2013  11:49 AM   10037007 06-Loreena_McKennitt-The_Book_Of_Secrets-La_Serenissima (1).mp3
-a---         1/20/2013  11:47 AM   10037007 06-Loreena_McKennitt-The_Book_Of_Secrets-La_Serenissima.mp3
-a---         1/20/2013  11:51 AM   16985204 07-Loreena_McKennitt-The_Book_Of_Secrets-Night_Ride_Across_The_Caucasus.mp3
-a---         1/20/2013  11:50 AM   13847459 08-Loreena_McKennitt-The_Book_Of_Secrets-DantesPrayer.mp3
-a---         1/20/2013  11:48 AM     252931 Loreena_McKennit_The_Book_Of_Secrets.pdf


I have two problems:
  1. I clicked some files twice, and now have duplicate files (La_Serenissima).
  2. The files have the wrong naming convention - I only want the MP3 to be named as the song.
I want to erase the duplicate files. I can find duplicate files by searching for any filename that contains (1), I do this by using the -match operator on each file. The match operator accepts a regular expression thus the funny escaping.
PS C:\Users\idvor_000\Downloads\music> dir | ? { $_ -match "\(1\)"}
    Directory: C:\Users\idvor_000\Downloads\music
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/20/2013  11:49 AM   10037007 06-Loreena_McKennitt-The_Book_Of_Secrets-La_Serenissima (1).mp3

Since my regular expression matches the files I want, I can pass those files to Remove-Item:
 PS C:\Users\idvor_000\Downloads> dir | ? { $_ -match "\(1\)"} | Remove-Item

Next, I want to rename my files to only have the song name. I can do this by splitting the filename on '-', and using the last element. I suspect the following line should work, and test it.
PS C:\Users\idvor_000\Downloads\music> dir | % {echo $_.Name.split('-')[-1]}
Prologue.mp3
The_Mummers_Dance.mp3
Skellig.mp3
The_Highwayman.mp3
La_Serenissima.mp3
Night_Ride_Across_The_Caucasus.mp3
DantesPrayer.mp3

Good that works, now instead of echo'ing lets actually rename the original file to the new name..
PS C:\Users\idvor_000\Downloads\music> dir | % {rename $_  $_.Name.split('-')[-1]}
PS C:\Users\idvor_000\Downloads\music> dir
    Directory: C:\Users\idvor_000\Downloads\music
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         1/20/2013  11:50 AM   13847459 DantesPrayer.mp3
-a---         1/20/2013  11:47 AM   10037007 La_Serenissima.mp3
-a---         1/20/2013  11:51 AM   16985204 Night_Ride_Across_The_Caucasus.mp3
-a---         1/20/2013  11:47 AM    8648447 Prologue.mp3
-a---         1/20/2013  11:47 AM   12026448 Skellig.mp3
-a---         1/20/2013  11:49 AM   20341141 The_Highwayman.mp3
-a---         1/20/2013  11:48 AM   12194913 The_Mummers_Dance.mp3

Yippy Skippy it works! Happy powershelling.

No comments: