{"id":69223,"date":"2005-08-08T13:39:00","date_gmt":"2005-08-08T13:39:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/08\/08\/how-can-i-list-all-the-songs-in-the-windows-media-library\/"},"modified":"2005-08-08T13:39:00","modified_gmt":"2005-08-08T13:39:00","slug":"how-can-i-list-all-the-songs-in-the-windows-media-library","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-songs-in-the-windows-media-library\/","title":{"rendered":"How Can I List All the Songs in the Windows Media Library?"},"content":{"rendered":"<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" class=\"nearGraphic\" \/><\/p>\n<p>Hey, Scripting Guy! How can I get a list of all the songs (and their artists) in the Windows Media Library?<\/p>\n<p>&#8212; KF<\/p>\n<p><img decoding=\"async\" height=\"5\" width=\"5\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" alt=\"Spacer\" border=\"0\" \/><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" class=\"nearGraphic\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" height=\"288\" width=\"120\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" align=\"right\" alt=\"Script Center\" border=\"0\" title=\"Script Center\" class=\"farGraphic\" \/><\/a><\/p>\n<p>Hey, KF. Three words: practice, practice, practice.<\/p>\n<p>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&hellip; well &hellip;.<\/p>\n<p>OK, never mind. Instead of practice, practice, practice, how about <i>these<\/i> three words: Use this script.<\/p>\n<p>By which we mean this script right here:<\/p>\n<pre class=\"codeSample\">Set objPlayer = CreateObject(\"WMPlayer.OCX\" )\n\nSet objMediaCollection = objPlayer.MediaCollection\nSet colSongList = objMediaCollection.getByAttribute(\"MediaType\", \"audio\")\n\nFor i = 0 to colSongList.Count - 1\n    Set objSong = colSongList.Item(i)\n    Wscript.Echo objSong.Name &amp; \" -- \" &amp; objSong.getItemInfo(\"WM\/AlbumArtist\")\nNext\n<\/pre>\n<p>The script begins by creating an instance of the <strong>WMPlayer.OCX<\/strong> 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:<\/p>\n<pre class=\"codeSample\">Set objMediaCollection = objPlayer.MediaCollection\n<\/pre>\n<p>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:<\/p>\n<pre class=\"codeSample\">Set colSongList = objMediaCollection.getByAttribute(\"MediaType\", \"audio\")\n<\/pre>\n<p>As you can see, all we do is call the <strong>getByAttribute<\/strong> method, passing two parameters:<\/p>\n<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" class=\"listBullet\">&bull;<\/td>\n<td class=\"listItem\">\n<p>&ldquo;MediaType&rdquo;, which represents the attribute we&rsquo;re interested in.<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" class=\"listBullet\">&bull;<\/td>\n<td class=\"listItem\">\n<p>&ldquo;audio&rdquo;, which represents the value of the attribute we&rsquo;re interested in.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In other words, &ldquo;Bring me back all the objects where the MediaType is equal to audio.&rdquo;<\/p>\n<p>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 <strong>Count<\/strong> 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.)<\/p>\n<p>Each time through the loop we bind to an individual audio file using this code:<\/p>\n<pre class=\"codeSample\">Set objSong = colSongList.Item(i)\n<\/pre>\n<p>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 <strong>getItemInfo<\/strong> method and specify the <strong>WM\/AlbumArtist<\/strong> attribute. Why? Well, just like your mom and dad used to tell you: because. That&rsquo;s just the way the Media Player object model works.<\/p>\n<p>When we run this script we get back information similar to this:<\/p>\n<pre class=\"codeSample\">Losing My Religion -- R.E.M.\nGarden Party -- Rick Nelson\nTeacher Teacher -- Rockpile\nLet's Spend the Night Together -- The Rolling Stones\nAnybody Seen My Baby? -- The Rolling Stones\nIt's Only Rock 'N Roll (But I Like It) -- The Rolling Stones\n<\/pre>\n<p>Cool, huh?<\/p>\n<p>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.&nbsp;<\/p>\n<p>And don&rsquo;t forget: practice, practice, practice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I get a list of all the songs (and their artists) in the Windows Media Library? &#8212; KF 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[123,3,5,192],"class_list":["post-69223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-multimedia","tag-scripting-guy","tag-vbscript","tag-windows-media-player-and-audio"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I get a list of all the songs (and their artists) in the Windows Media Library? &#8212; KF 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=69223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=69223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}