'Tags all MP3 files in an entire folder
Const ID3_GENRE = "A Capela,Acid,Acid Jazz,Acid Punk,Acoustic,Alternative,AlternRock,Ambient,Anime,Avantgarde,Ballad,Bass,Beat,Bebob,Big Band,Black Metal,Bluegrass,Blues,Booty Brass,BritPop,Cabaret,Celtic,Chamber Music,Chanson,Chorus,Christian Gangsta Rap,Christian Rap,Christian Rock,Classic Rock,Classical,Club-House,Club,Comedy,Contemporary Christian,Country,Crossover,Cult,Dance,Dance Hall,Darkwave,Death Metal,Disco,Dream,Drum & Bass,Drum Solo,Duet,Easy Listening,Electronic,Ethnic,Euro-House,Euro-Techno,Eurodance,Fast Fusion,Folk-Rock,Folk,Folklore,Freestyle,Funk,Fusion,Game,Gangsta,Goa,Gospel,Gothic,Gothic Rock,Grunge,Hard Rock,Hardcore,Heavy Metal,Hip-Hop,House,Humour,Indie,Industrial,Instrumental,Instrumental Pop,Instrumental Rock,Jazz,Jazz+Funk,JPop,Jungle,Latin,Lo-Fi,Meditative,Merengue,Metal,Musical,National Folk,Native American,Negerpunk,New Age,New Wave,Noise,Oldies,Opera,Other,Polka,Polsk Punk,Pop-Folk,Pop,Pop/Funk,Porn Groove,Power Ballad,Pranks,Primus,Progressive Rock,Psychadelic,Psychedelic Rock,Punk,Punk Rock,R&B,Rap,Rave,Reggae,Retro,Revival,Rhythmic Soul,Rock,Rock & Roll,Salsa,Samba,Satire,Showtunes,Ska,Slow Jam,Slow Rock,Sonata,Soul,Sound Clip,Soundtrack,Southern Rock,Space,Speech,Swing,Symphonic Rock,Symphony,SynthPop,Tango,Techno-Industrial,Techno,Terror,Thrash Metal,Top 40,Trailer,Trance,Tribal,Trip-Hop,Vocal"

Main

Sub Main
Dim ws, fs, java, fil, fol, fils
Dim strFolder, strClassPath, strGenre, strTitle, strAlbum, strArtist, strTrack, strData
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set ws = CreateObject("WScript.Shell")
	
	'Check for Java
	On Error Resume Next
	strData = ""
	Err.Clear
	Set java = ws.Exec("Java.exe")
	If Err.Number = 0 Then
		Do While java.Status = 0
			WScript.Sleep 100
		Loop
		strData = java.StdOut.ReadAll
	End If
	If Len(strData) < 100 Then
		MsgBox "Please download and install ""Java"" from http://java.com/"
		Exit Sub
	End If	
	'Find the location of the needed JAR file
	strClassPath = VersionOfFileInThisDir("jampal*.jar")
	If strClassPath = "" Then
		MsgBox "Place this script in a folder with some version of ""jampal.jar"" (from http://jampal.sourceforge.net/)"
		Exit Sub
	End If
	strClassPath = """" & strClassPath & """"
	'Make sure we were passed a folder to work in
	If WScript.Arguments.Count = 0 Then
		ToggleRightClick()
		Exit Sub
	End If
	strFolder = fs.GetAbsolutePathName(WScript.Arguments(0))
	If Not fs.FolderExists(strFolder) Then
		MsgBox "That isn't a folder!"
		Exit Sub
	End If
	ws.CurrentDirectory = strFolder
	'Get the GENRE
	Do
		strData = InputBox("GENRE", "GENRE", "Speech")
		If strData = "" Then 
			strData = """"""
			Exit Do
		End If
		If Left(strData, 1) <> """" Then strData = """" & strData
		If Right(strData, 1) <> """" Then strData = strData & """"
		For Each strGenre In Split(ID3_GENRE, ",")
			If """" & strGenre & """" = strData Then Exit Do
		Next
		ListOfGenres
	Loop
	strGenre = strData
	'Get the ARTIST
	strData = InputBox("ARTIST", "ARTIST", "")
	If strData = "" Then strData = """"""
	If Left(strData, 1) <> """" Then strData = """" & strData
	If Right(strData, 1) <> """" Then strData = strData & """"
	strArtist = strData
	'Get the ALBUM
	strData = InputBox("ALBUM", "ALBUM", "")
	If strData = "" Then strData = """"""
	If Left(strData, 1) <> """" Then strData = """" & strData
	If Right(strData, 1) <> """" Then strData = strData & """"
	strAlbum = strData
	'Get the TITLE
	strData = InputBox("TITLE", "TITLE", Replace(strAlbum, """", ""))
	If strData = "" Then strData = """"""
	If Left(strData, 1) <> """" Then strData = """" & strData
	If Right(strData, 1) <> """" Then strData = strData & """"
	strTitle = strData
	'Iterate through all files
	Set fol = fs.GetFolder(strFolder)
	Set fils = fol.Files 
	For Each fil In fils
		'Get the TRACK
		strData = FirstNumber(Replace(fil.Name, "-", ""))
		If strData <> "" Then 
			strData = Left(strData, 4)
			strData = CLng(strData)
			If Left(strData, 1) <> """" Then strData = """" & strData
			If Right(strData, 1) <> """" Then strData = strData & """"
		Else
			strData = """"""
		End If
		strTrack = strData
		'Run the Java program
		Status fil.Name
		ws.Run "java.exe -cp " & strClassPath & " pgbennett.id3.TagUpdate -GENRE " & strGenre & " -TRACK " & strTrack & " -TITLE " & strTitle & " -ARTIST " & strArtist & " -ALBUM " & strAlbum & " """ & fil.Path & """", 0, True
	Next
End Sub

Sub Status(strMessage)
	If Lcase(Right(Wscript.FullName, 12)) = "\cscript.exe" Then
		Wscript.Echo strMessage
	End If
End Sub

Function FirstNumber(strNumber) 'As String
'Returns the first number in a string. For example
'CA0567-2 returns 0567
'If there are no numbers, an empty string is returned.
Dim strBuffer
Dim intCounter
Dim blnNumeric
	If IsNull(strNumber) Then
		FirstNumber = ""
		Exit Function
	End If
	If strNumber = "" Then
		FirstNumber = ""
		Exit Function
	End If
	strBuffer = ""
	blnNumeric = False
	For intCounter = 1 To Len(strNumber)
		If IsNumeric(Mid(strNumber, intCounter, 1)) Then
			blnNumeric = True
			strBuffer = strBuffer & Mid(strNumber, intCounter, 1)
		Else
			If blnNumeric Then Exit For
		End If
	Next
	FirstNumber = strBuffer
End Function

Function VersionOfFileInThisDir(strStarDelimitedName)
Dim fs, fol, fils, fil, strHead, strTail, strFile
	Set fs = CreateObject("Scripting.FileSystemObject")
	Set fol = fs.GetFolder(fs.GetParentFolderName(WScript.ScriptFullName))
	Set fils = fol.Files
	strHead = Split(strStarDelimitedName, "*")(0)
	strTail = Split(strStarDelimitedName, "*")(1)
	strFile = ""
	For Each fil In fils
		If Lcase(Left(fil.Name, Len(strHead))) = Lcase(strHead) Then
			If Lcase(Right(fil.Name, Len(strTail))) = Lcase(strTail) Then
				strFile = fil.Path
			End If
		End If
	Next
	VersionOfFileInThisDir = strFile
End Function

Sub ToggleRightClick()
' Adds or deletes this script as a right-click option for "Directory"
Dim ws, fs, strKey
	Set ws = CreateObject("Wscript.Shell")
	Set fs = CreateObject("Scripting.FileSystemObject")
	
	On Error Resume Next
	
	strKey = "HKEY_CLASSES_ROOT\Directory\shell\" & fs.GetBaseName(WScript.ScriptName) & "\"
	If RightClickEnabled(strKey) Then
		ws.RegDelete strKey & "command\"
		ws.RegDelete strKey
		MsgBox "Right-Click option on folders for this script has been REMOVED",,fs.GetBaseName(WScript.ScriptName)
	Else
		ws.RegWrite strKey & "command\", "cscript.exe """ & WScript.ScriptFullName & """ ""%1""", "REG_EXPAND_SZ"
		MsgBox "Right-Click option on folders for this script has been ADDED",,fs.GetBaseName(WScript.ScriptName)
	End If
End Sub

Function RightClickEnabled(strKey)
Dim ws, fs
	Set ws = CreateObject("Wscript.Shell")
	On Error Resume Next
	RightClickEnabled = Eval("" <> ws.RegRead(strKey & "command\"))
End Function


Sub ListOfGenres
Dim fs, web, doc
Dim strFile, dtTime
	On Error Resume Next
	Set web = CreateObject("InternetExplorer.Application")
	If web Is Nothing Then Exit Sub
	web.Width = 250
	web.Height = 400
	web.Offline = True
	web.AddressBar = False
	web.MenuBar = False
	web.StatusBar = False
	web.Silent = True
	web.ToolBar = False
	web.Navigate "about:blank"
	'Wait for the browser to navigate to nowhere
	dtTime = Now
	Do While web.Busy
		'Don't wait more than 5 seconds
		Wscript.Sleep 100
		If (dtTime + 5/24/60/60) < Now Then
			ChooseOne = ""
			web.Quit
			Exit Sub
		End If
	Loop
	'Wait for a good reference to the browser document
	Set doc = Nothing
	dtTime = Now
	Do Until Not doc Is Nothing
		Wscript.Sleep 100
		Set doc = web.Document
		'Don't wait more than 5 seconds
		If (dtTime + 5/24/60/60) < Now Then
			web.Quit
			Exit Sub
		End If
	Loop
	'Write the HTML form
	doc.Write "<html><head><title>ID3 Genres</title></head>"
	doc.Write "<body><h1>ID3 Genres</h1><br>"
	doc.Write Replace(ID3_GENRE, ",", "<br>")
	doc.Write "</body></html>"
	'Show the form
	web.Visible = True
End Sub
