Reading MP3 ID3 Tags in WSH

ID3 is a popular audio file data tagging format used to store information about a sound file. The MP3 file format uses ID3 tags to store title, artist, and other track information. Unfortunately, WSH doesn’t provide a way to read ID3 tags without the help of third-party components, but today I’m going to show you a native workaround that will allow you to retrieve this valuable information.

ID3 tags are support by common software programs such as Windows Media Player, iTunes, MusicMatch, and common hardware such as iPod, Creative Zen, and Sony Walkman.

Windows does contain internal functionality for reading and writing ID3 tags, however there is no scripting interface for accessing this functionality through WSH.  But that doesn’t suggest that it isn’t possible.  As a scripter you sometimes have to think outside of the box.

In the Windows environment, ID3 tags are most evident in Windows Explorer when browsing a folder containing music files.  If you use Detail View (by selecting Details from the View menu) you will see a series of columns appear.  Many of these columns match the various ID3 tags and this is the information that they display.

While we may not be able to access ID3 tags directly, the Shell object does allow us to read the values of these columns.  Distinguishing between them now becomes the trick—and it just wouldn’t be Windows if there wasn’t some incompatibility across platforms. As you will see, these columns are identified by number. Microsoft further aids our efforts by leaving these column IDs undocumented.

But before we get too involved, let’s take a look at these columns and how to read their values.

{mospagebreak title=Retrieving file details in WSH}

As I mentioned, we’re going to rely on the Windows Shell and its scripting interface for retrieving the information we want.  I’m going to tell you the column numbers you need to use later in this article, but for learning purposes, let’s see how I was able to find them.

strFolder = "C:Documents and SettingsNilpoMy DocumentsMy Music"

We begin our script by creating the variable strFolder and setting it to a folder path containing MP3 files.

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.NameSpace(strFolder)

Next we connect to the Windows Shell object and use its NameSpace method to return a folder object.

object.GetDetailsOf(file, colId)

The Folder object has a GetDetailsOf method that allows us to read the details of files within a folder. It has two required parameters where file is a reference to a File object within the folder and colId is an integer representing the detail column to read.

Ordinarily, at this point we would use the Folder object’s Items property to return a collection of File objects and then iterate through them, reading the respective details; but as it turns out, if you provide a non-existent object, you can return the actual column headings instead.

For N = 0 To 100

   WScript.Echo N & vbTab & objFolder.GetDetailsOf(Nothing, N)

Next

Depending upon your system and the types of files contained in the folder, there can be well over one hundred different file properties available. For our purposes we’re only going to look at the first one hundred.

Here, I’m using a simple For…Next loop count from 0 to 100. Notice that I’ve used the Nothing keyword in place of a File object in the first parameter. Remember that VBScript uses the Nothing keyword to represent a disconnected or invalid object reference.

Running the script will return a list of all of the column headings for columns 0 through 100.  You can run this script on your own if you’re curious about the different details that are available, but I’ve included a table below with the ones that we are interested in.

Windows XP and Windows Server 2003 use the same column IDs while Vista has its own renumbering. Some of these fields are also available on Windows 2000 and earlier; however, they may not include all of them.

Tag Name

Windows XP and
Windows Server 2003

Windows Vista

Name

0

0

Size

1

1

Type

2

2

Title

10

21

Comments

14

24

Artist

16

13

Album Title

17

14

Year

18

15

Track Number

19

27

Genre

20

16

Duration

21

36

Bit Rate

22

28

{mospagebreak title=Constructing the Script}

Once you know the column ID for the detail information that you want, you can use the GetDetailsOf method to return that information.  You’re actual working script is going to begin much like the one we’ve just used.

strFolder = "C:Documents and SettingsNilpoMy DocumentsMy Music"

 

Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.NameSpace(strFolder)

Again, you’ll need to specify a folder to work with.  Then you’ll connect to the Windows Shell object and return a Folder object.

Set colFiles = objFolder.Items

Next, you’ll use the Folder object’s Items property to return a collection of File objects that are contained in the folder.  Now it’s just a matter of iterating through the collection.

Because you may want to run this script against multiple folders or add recursive searching, I’m going to add a little bit of error-handling.

If Not objFolder Is Nothing Then

The remainder of the script is enclosed in this If statement. This will prevent any errors if the script attempts to load a folder path that doesn’t exist. In my case, I keep this script at all times to maintain a database of my MP3 library. From time to time, folders get moved or deleted. With this line in place, I don’t have to worry if I forget to update my script.

For Each objFile In colFiles

   strType = objFolder.GetDetailsOf(objFile, 2)

   If strType = "MP3 Format Sound" Then

Next, I’ll begin a For Each loop to iterate through the collection objects. This will process each file in our folder. Again, I’ve chosen to do a little extra work. I’m first using the GetDetailsOf method to determine the file type. Then I’m using an If statement to check that the file is an MP3. Including this If statement will allow the script to run on folders that contain additional files by filtering out the MP3s that we’re looking for.

       WScript.Echo "Name:", objFile.Name & ".mp3"

       WScript.Echo "Size:", objFolder.GetDetailsOf(objFile, 1)

       WScript.Echo "Title:", objFolder.GetDetailsOf(objFile, 10)

       WScript.Echo "Comments:", objFolder.GetDetailsOf(objFile, 14)

       WScript.Echo "Artist:", objFolder.GetDetailsOf(objFile, 16)

       WScript.Echo "Album Title:", objFolder.GetDetailsOf(objFile, 17)

       WScript.Echo "Year:", objFolder.GetDetailsOf(objFile, 18)

       WScript.Echo "Track Number:", objFolder.GetDetailsOf(objFile, 19)

       WScript.Echo "Genre:", objFolder.GetDetailsOf(objFile, 20)

       WScript.Echo "Duration:", objFolder.GetDetailsOf(objFile, 21)

       WScript.Echo "Bit Rate:", objFolder.GetDetailsOf(objFile, 22)

       WScript.Echo "**************************************************"

Now it’s a simple matter of using the GetDetailsOf method to retrieve the file details associated with the ID3 fields. This example uses the column IDs for Windows XP and Windows Server 2003. If you are using Windows Vista, you should refer to the table on the last page and change the column IDs accordingly.

       End If

   Next

End If

We can wrap up the script by closing all of our statements.  Running the script will process each MP3 file in the folder that you’ve specified and print a list of details for each file.

{mospagebreak title=Working with the Data}

You now have a workaround that will allow you to retrieve the information contained in the ID3 tags of MP3 files. There’s really not much point in printing them back to the screen, so you should continue this script to serve your own purpose.

Maybe you want to create an Access database with all of your MP3 data.  Perhaps you could create an Excel worksheet to sort the data. If you’re really creative, you could even use this information to remove duplicate songs across multiple folders, even if the files have different names!

Whatever you do from here is up to you. If you’re interested in using Access or Excel to catalog your MP3 library, be sure to catch the rest of this series where I’ll be demonstrating both of these concepts.

It’s also important to remember that this concept is not limited to MP3 files and ID3 tags (which, by the way, are also available for Windows-formatted audio and video files). It can be used to retrieve any file details that are available from the Details view in Windows Explorer. That means that you can return a file’s owner, attributes, image dimensions, and more.

Take the time to run the first example script we created so you can see what types of information are available. Be sure to run it against folders containing other types of files as well. There is much information to be had, but whatever you do, remember to have fun. Until next time, keep coding!

4 thoughts on “Reading MP3 ID3 Tags in WSH

  1. Did you know that you can use WSH and the Windows Shell object to read the ID3 information tags on MP3 files? Read my article to learn how.

  2. What would be really usefull is a way to be able to add more tabs on the columns in windows explorer.. To be more specific, the “Composer”, or the “Album Artist” fields in ID3v2 are not available as tabs in Windows Explorer. I’m not sure wether Windows can read them but has no option to display them or can’t read em at all..

    I have been trying to find ways to do that with no luck.. These fields, if visible in W.Expl., could contain information such as star-rating of a song, something which is now only achievable using media libraries (databases) which are a real pain in the a$$. Imagine how great that would be, sorting your music by star-rating without having to depend on crappy and buggy software such as Windows Media Player or Winamp!

  3. there is such a thing as file association.
    In my case an mp3 file wasn’t “MP3 Format Sound” but “Winamp media file”

    My tip:

    add the following line

    strType = objFolder.GetDetailsOf(objFile, 2)
    ==> WScript.Echo strType
    If (strType = “MP3 Format Sound”) Then

    look at the output and change the code to (for example)

    strType = objFolder.GetDetailsOf(objFile, 2)
    ‘WScript.Echo strType
    If (strType = “MP3 Format Sound”) OR (strType = “Winamp media file”) Then

    and it will work without a problem 8)

[gp-comments width="770" linklove="off" ]