Skip Navigation LinksHome > View Post

Tonight PowerShell saved my music library

I recently managed to mess up my library of mp3's and ended up with two copies of everything. I had a copy of each named '<Track No> - <Track Name>.mp3' and a copy named '<Artist> - <Album> - <Track No> - <Track Name>.mp3'. Since I already have the Artist and Album in the folder structure I wanted to get rid of all files with the latter name format.

Regular Expressions were clearly in a good position to help me here and it was tempting to just write a quick Console Application to clear this up for me. But no, we must hold back from writing little apps any longer! PowerShell is here to save the day.

There are many, many better qualified people out there writing about PowerShell so I'll spare you the full lowdown and just show you how it solved my problem. If you like what you see, you'll check out the links at the bottom.

First of all we need to create the regex. The approach I took was to find all strings that didn't start with "## -"(where the # is any numeric digit). Fortunately, this is pretty easy: "^[^\d\d -]".

To create this in PowerShell we do this:

$re = [regex]"^[\d\d -]"

This also assigned the regex to a variable called $re so re can reuse it later.

First, lets list all mp3 files in my music folder:

dir -Filter "*.mp3" -recurse

That should be easy enough to understand for anyone who's used the cmd window before. This will list all .mp3 files in my current music folder and, thanks to the -recurse switch, any subfolders too.

Next, we need to limit this to list only files that match my regex and we do that by piping up a 'where clause':

dir -Filter "*.mp3" -recurse | where {$_.Name -match $re}

Executing this command will list all the files that I want deleted so it's important to check them before proceeding.

Finally, to delete them, we simply pipe this statement up to the del cmdlet.

dir -Filter "*.mp3" -recurse | where {$_.Name -match $re} | del

And we're done, only deleting the files that match my regex. Also, if we wanted to be extra careful and check every file before deletion we could easily add the -confirm switch to the del cmdlet. Here's that example in its full glory.

$re = [regex]"^[^\d\d -]"
dir -Filter "*.mp3" -recurse | where {$_.Name -match $re} | del -confirm

How easy was that?


Tags: Powershell

Josh Post By Josh Twist
10:09 AM
29 Jul 2007

» Next Post: More links on PowerShell
« Previous Post: Inheriting SnapsToDevicePixels

Comments are closed for this post.

Posted by Josh @ 29 Jul 2007 10:21 AM
PS - we can also bask in the amusement that is the recapitalised name: Windows PowersHell :)

Posted by James World @ 29 Aug 2007 5:10 AM
You mentioned the importance of checking the list of files for deletion.

Another way to check a potentially dangerous operation in powershell is to use the -whatif switch. This tells you what the command would do if you executed it.

This is a bit cleaner I think. Example:

$re = [regex]"^[^\d\d -]"
dir -Filter "*.mp3" -recurse | where {$_.Name -match $re} | del -whatif

Posted by Josh @ 29 Aug 2007 11:58 AM
Thanks James,

Funny you mention that - I found the -WhatIf option just yesterday when using PowerShell to rename a bunch of files using the Rename-Item (rni) cmdlet!

© 2005 - 2015 Josh Twist - All Rights Reserved.