Reading MP3 ID3 Tags in WSH - Constructing the Script
(Page 3 of 4 )
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.
Next: Working with the Data >>
More Windows Scripting Articles
More By Nilpo