Using WSH to Catalog MP3 Files - Finishing the script
(Page 4 of 4 )
As I noted in my last article, the column IDs that need to be read by the GetDetailsOf method will vary across different versions of Windows. In order to maintain compatibility I’ve chosen to use the SetColIds subroutine to assign the proper column IDs to variables depending upon the current version of Windows.
Sub SetColIds
If GetOSVersion = "XP" Or GetOSVersion = "2003" Then
intSize = 1
intTitle = 10
intComments = 14
intArtist = 16
intAlbumTitle = 17
intYear = 18
intTrack = 19
intGenre = 20
intDuration = 21
intBitRate = 22
We’ll begin by creating the subroutine structure. This subroutine simply uses an If statement to assign variable values for each OS version. Since Windows XP and Windows Server 2003 use the same column IDs, we can combine them into the same branch of our If statement using the Or operator.
ElseIf GetOSVersion = "Vista" Then
intSize = 1
intTitle = 21
intComments = 24
intArtist = 13
intAlbumTitle = 14
intYear = 15
intTrack = 27
intGenre = 16
intDuration = 36
intBitRate = 28
End If
End Sub
The Else branch is used to assign variable values that are compatible with Windows Vista. Notice that both conditional statements rely on the result from a GetOSVersion function. This is a custom function that we’ll need to create in order to determine the OS version.
Function GetOSVersion
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!" & strComputer & "rootcimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
strVersion = objOperatingSystem.Caption
Exit For
Next
If InStr(strVersion, "2000") > 0 Then GetOSVersion = "2000"
If InStr(strVersion, "XP") > 0 Then GetOSVersion = "XP"
If InStr(strVersion, "2003") > 0 Then GetOSVersion = "2003"
If InStr(strVersion, "Vista") > 0 Then GetOSVersion = "Vista"
End Function
The GetOSVersion function looks complicated, but it’s actually pretty simple if you break down the steps. It begins by using WMI to poll the name of the operating system. This is done by using the Caption property of the Win32_OperatingSystem class. This class does return a collection of instances, so we have to use a For Each loop to process it properly.
The Exit For statement ensures that the function only returns information for the first instance in a case where a system has multiple operating systems installed. The first instance will be the first “bootable” operating system.
Finally, a set of If statements is used to determine which operating system is being used. VBScript’s InStr function searches the caption string for identifying information. Once the version is determined, it is returned by the function as a short string.
And that’s all there is to it. You’ve just created a script that is capable of looking in a folder, finding any MP3 files, reading their ID3 tags, and then storing that information in an Access-compatible database. By pointing this script to each of your music folders, you can use it to create a catalog of all your music files.
You can use this database in any way you wish. Perhaps you just want an easy way to maintain a music library. Or maybe you have a large collection of music and you’re looking for a better way to manage it. Whatever your need, this script should put you well in the direction you want to go.
How about a scripting challenge? Can you think of a way to create an HTML application to serve as a GUI interface for managing your MP3 catalog? You could have drop down boxes for artist selection and album names and have it return appropriate song titles. Or, you could add a Browse… button that would allow you to select new folders for adding to your database.
The possibilities are endless. Experiment with this code and see what you can accomplish. Until next time, keep coding!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |