How Can I List All the Songs in the Windows Media Library?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I get a list of all the songs (and their artists) in the Windows Media Library?

— KF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KF. Three words: practice, practice, practice.

You know, the old joke about how do you get to Carnegie Hall and this is kind of a music-related question and Carnegie Hall is where they play music and we were just trying to be cute and… well ….

OK, never mind. Instead of practice, practice, practice, how about these three words: Use this script.

By which we mean this script right here:

Set objPlayer = CreateObject("WMPlayer.OCX" )

Set objMediaCollection = objPlayer.MediaCollection
Set colSongList = objMediaCollection.getByAttribute("MediaType", "audio")

For i = 0 to colSongList.Count - 1
    Set objSong = colSongList.Item(i)
    Wscript.Echo objSong.Name & " -- " & objSong.getItemInfo("WM/AlbumArtist")

The script begins by creating an instance of the WMPlayer.OCX object, which turns out to be the way you instantiate Windows Media Player in VBScript. We then use this line of code to make a connection to the Media Library:

Set objMediaCollection = objPlayer.MediaCollection

As you probably know, the Media Library can contain all sorts of things: JPEG pictures, video files, audio files, etc. Because we only care about music files (like .MP3 and .WMA files) we use this line of code to return a subset of the Media Library, a subset containing only audio files:

Set colSongList = objMediaCollection.getByAttribute("MediaType", "audio")

As you can see, all we do is call the getByAttribute method, passing two parameters:

“MediaType”, which represents the attribute we’re interested in.

“audio”, which represents the value of the attribute we’re interested in.

In other words, “Bring me back all the objects where the MediaType is equal to audio.”

The getByAttribute method returns an array of media items, each one representing an individual audio file. To retrieve information about these media items we need to set up a For Next loop that loops from 0 to the Count of the media items minus 1. (As with most arrays in VBScript, the first item in our array is item 0; therefore the last item will be the number of items minus 1. For example, if we have 100 items in the array then the last item will be number 99.)

Each time through the loop we bind to an individual audio file using this code:

Set objSong = colSongList.Item(i)

For each audio file we then echo back the name and the artist. You might note the crazy way we have to get the artist name: we have to call the getItemInfo method and specify the WM/AlbumArtist attribute. Why? Well, just like your mom and dad used to tell you: because. That’s just the way the Media Player object model works.

When we run this script we get back information similar to this:

Losing My Religion -- R.E.M.
Garden Party -- Rick Nelson
Teacher Teacher -- Rockpile
Let's Spend the Night Together -- The Rolling Stones
Anybody Seen My Baby? -- The Rolling Stones
It's Only Rock 'N Roll (But I Like It) -- The Rolling Stones

Cool, huh?

We realize that this script is a bit different from most of the scripts we cover in this column, and we realize that we might have scrimped on the explanation from time-to-time. 

And don’t forget: practice, practice, practice.


Discussion is closed.

Feedback usabilla icon