Visual Basic Script - Listing MP3 Files
Author: jason
Date: 2004-06-01
Category: Technical

In this VBScript tutorial we will search for MP3 files, write them to a HTML file, then open the file with Internet Explorer.

1. Check a folder and subfolders for files
2. Get all the .mp3 files and write them to a HTM file
3. Open the file when we're done

Here's the code (mp3-list.vbs)
Code

Option Explicit

Dim FileSys, WrFiles
Dim strInput, strTarget, strFullPath, IExec

strInput= InputBox("Please Enter a starting folder", "User Input","")

strTarget = "\mp3-list.htm"

Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WrFiles = FileSys.CreateTextFile(strInput & strTarget, True)

GetDir strInput

Sub GetDir(dir)
Dim oFile, oFiles
Dim oFolder, oFolders
Dim FolderItem, FileItem

Set oFolder=FileSys.GetFolder(dir)
Set oFolders=oFolder.SubFolders
Set oFiles=oFolder.Files

For each FolderItem in oFolders
GetDir(FolderItem)
Next

FileItem=0
For each FileItem in oFiles
If Right(FileItem.Name,4)=".mp3" Then
WrFiles.WriteLine("<b>" & FileItem.Name & "</b> (" & dir & ") " & "<br />")
End If
next
End Sub

WrFiles.Close

Set IExec = CreateObject("InternetExplorer.Application")
strFullPath = strInput & strTarget
IExec.navigate strFullPath
IExec.visible=1


Not too much more complicated then what we've already been through. But there are a few more elements. Lets step through it.

Sets VBScript into a stricter mode
Code

Option Explicit


Here we set up our variables. "FileSys" will hold the file system object. "WrFiles" writes into our text file. "strInput" will hold the user's input from the popup box. "strTarget" will hold our HTML file. "strFullPath" with hold the "strInput" and the "strTarget". And IExec will execute/open our HTML file when we're done.
Code

Dim FileSys, WrFiles
Dim strInput, strTarget, strFullPath, IExec


This will make a message box, and prompt the user to enter a directory/folder to search. The first part is the message inside the window. The second part (separated by commas) is the title bar. The third part is what will appear in the text area. We can put a default path in here if we want, especially if we'll be searching the same dir over and over. Saves our fingers some typing...
Code

strInput= InputBox("Please Enter a starting folder", "User Input","")


This maps our HTML file. The first slash is important, though I could have added it in another place with a & "\"
Code

strTarget = "\mp3-list.htm"


We set up the file system object. Then we use the user input and the target html information. We're going to place the HTM file into the directory the user specified.
Code

Set FileSys = CreateObject("Scripting.FileSystemObject")
Set WrFiles = FileSys.CreateTextFile(strInput & strTarget, True)


Here we are calling a sub-routine. The sub is GetDir with the strInput for the folder we want to look in.
Code

GetDir strInput


The sub is named GetDir and the (dir) sets the argument to fetch the strInput and hold it. These variables hold the file and folder item names which we'll call below.
Code

Sub GetDir(dir)
Dim oFile, oFiles
Dim oFolder, oFolders
Dim FolderItem, FileItem


We setup oFolder to get the file system object from way above, and open the folder from the argument passed with the sub-routine call. On the middle line we get the subfolders out of the directory. And on the last line we get the files out.
Code

Set oFolder=FileSys.GetFolder(dir)
Set oFolders=oFolder.SubFolders
Set oFiles=oFolder.Files


Here we step through each item, and call the GetDir sub-routine again to step through the subfolders. This time we're not writing the folder info. If there isn't a mp3 in it, then we don't care.
Code

For each FolderItem in oFolders
GetDir(FolderItem)
Next


Run through the files in each of the subfolders. Here is the key to our adventure: " Right(FileItem.Name,4)=".mp3" ". On the right of the file name, count back 4 and look for .mp3. Sweet, eh? We also add some HTML elements into the line we are writing. We separate these elements from VBS by putting quotes around them, and putting an & where we are joining something together.
Code

FileItem=0
For each FileItem in oFiles
If Right(FileItem.Name,4)=".mp3" Then
WrFiles.WriteLine("<b>" & FileItem.Name & "</b> (" & dir & ") " & "<br />")
End If
next


When finished with everything we can end the sub-routine.
Code

End Sub


Close out the file
Code

WrFiles.Close


Create the Internet Explorer object. This will let us open the HTML file.
Code

Set IExec = CreateObject("InternetExplorer.Application")


Here we get the full path to the HTML file
Code

strFullPath = strInput & strTarget


And now we find it with the IE object and open it up. The 1 is true, the document should be visible.
Code

IExec.navigate strFullPath
IExec.visible=1


That's it. You now have a MP3 list maker. It's still not very pretty. In the next lesson we'll make it a little prettier.

Reference: http://www.scit.wlv.ac.uk/appdocs/chili-asp/html/vbscript_language_reference.htm

Reference: http://www.winnetmag.com/WindowsScripting/Article/ArticleID/7667/7667.html



jason @ jasonthomasfrance.com - www.masterstationlog.com - copyright 2009