Sunday, February 10, 2013

PowerShell: Cleaning up Music File Names

I've been helping a friend manage his plethora of digital music.  Anyone who's acquired music has noticed the many ways filenames have been created.  Since Windows can display extra columns of information that coincide with MP3 tags, he decided the song names should be the filenames as the files would stay in the Artist/Album folders.

Here's a sample of the files original names:

After much research, I found the Title tag was usually a cleaner file name source than the actual file name.  I also noticed that hyphens were used a bunch and the section after the last hyphen was the song's name.  I used these two findings combined with character filters to come up with this:

PowerShell versions 1 and 2 balk at the [brackets] in a filename unless you perform some trickery.  PowerShell 3 has overcome this limitation -- but I'm using PS 2.0 for this script.  The script is not perfect but at least gets you in the general area.  The script does NOT -recurse (trickle down subfolders) since it's best to check your progress one folder at a time.

Here is the script if you'd like to give it a shot.  Place the .PS1 file with your other scripts and run it from the music folder to be affected.


$shell = new-object -com shell.application
$dirname = (get-item .\).fullname
$filename = (get-item .\*)|foreach-object{$_.name}

foreach($file in $filename){

#original filename holder

$fileholder = $file

#get the songs Title (item number 21 (if it exists)) property from the file

$shellfolder = $shell.namespace($dirname).parsename($file)
$title = $shell.namespace($dirname).getdetailsof($shellfolder,21)

#if the title isn't empty, use it for the filename.
#Note: This is specifically for MP3's; you'd have to add other lines for 
#other music file extensions

if ($title -gt 0){$file = ($title + '.mp3')}

#clean extraneous characters from the filename
#experiment by adding filters which serve you best

$file2 = $file -replace ('^[0-100]','')
$file3 = $file2 -replace ('.mp33','.mp3')
$file4 = $file3 -replace (' - ','-')
$file5 = $file4 -replace ('   ',' ')
$file6 = $file5 -replace ('  ',' ')
$file7 = $file6 -replace ('\(','')
$file8 = $file7 -replace ('\)','')

#Split the filename at the hyphen and only keep the last portion
#since that's normally the song name. -1 means get the last one.

$file9 = $file8.split('-')[-1]

#rename the file after all the modifications

rename-item -path ($dirname + '\' + $fileholder) -newname $file9
}

5 comments:

fracmoe said...

Nice script. Where can I read more about how you found Title was item property 21?

George said...

Thanks Fracmoe. I'm working on file retention for my job so asked on how to access those extra file properties and was pointed towards using -com shell.application. I found two good articles: http://tiki.gmartin.org/tiki-view_blog_post.php?postId=165 and http://powershell.com/cs/blogs/tobias/archive/2011/01/07/organizing-videos-and-music.aspx and performed lots of searches of Microsoft sites until I found a solution I liked. Since the writing of this article, I've continued using the properties to create a music folder system based on genre, artist, and album names. I'll posted it once I've won the war on brackets.

fracmoe said...

thanks!

Unknown said...

you can also use following script to get the Mp3 metadata - https://geekeefy.wordpress.com/2016/10/15/powershell-get-mp3mp4-files-metadata-and-how-to-use-it-to-make-you-life-easy/

George said...

Thanks Prateek. It's been 3 years since I played with this script and I'm sure there's better resources out there now.