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

Hey, Scripting Guy! Question

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

— KF

Spacer Hey, Scripting Guy! Answer Script Center

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

You know, the former joke well-nigh how do y'all go to Carnegie Hall and this is kind of a music-related question and Carnegie Hall is where they play music and nosotros were just trying to be beautiful and… well ….

OK, never heed. Instead of practice, practice, practise, how about these 3 words: Apply this script.

By which we hateful this script right here:

Set objPlayer = CreateObject("WMPlayer.OCX" )  Set objMediaCollection = objPlayer.MediaCollection Prepare colSongList = objMediaCollection.getByAttribute("MediaType", "sound")  For i = 0 to colSongList.Count - i     Prepare objSong = colSongList.Detail(i)     Wscript.Echo objSong.Proper noun & " -- " & objSong.getItemInfo("WM/AlbumArtist") Side by side        

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 employ this line of code to make a connexion to the Media Library:

Ready objMediaCollection = objPlayer.MediaCollection        

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

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

As you can meet, all we exercise is call the getByAttribute method, passing ii parameters:

"MediaType", which represents the attribute we're interested in.

"sound", which represents the value of the aspect nosotros'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 remember information about these media items we demand to set up a For Next loop that loops from 0 to the Count of the media items minus i. (As with most arrays in VBScript, the first item in our assortment is item 0; therefore the last item will be the number of items minus one. For example, if nosotros accept 100 items in the assortment then the last item will be number 99.)

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

Set up objSong = colSongList.Item(i)        

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

When we run this script nosotros go back information similar to this:

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

Absurd, huh?

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

And don't forget: practice, exercise, practice.