' ************************************************************************************
' Script:		wma2mp3.vbs
' Descripton:	Convert wma files to mp3 using switch.exe (not protected encrypted wma)
'				Get Switch from http://nch.com.au/switch/index.html
' Author:		Gary Evans - http://www.gary-evans.com
' Date:			22 May 2005
' ************************************************************************************
' Updates/Changes
' Date - Initials - Comment
' ************************************************************************************
' Disclaimer:  Use at your own risk - No support, no warranty, no nothing not even implied
' If it's useful then I'm glad, if not then sorry but such is life.
' This will NOT convert protected wma files and will NOT remove DRM etc
' ************************************************************************************
' Usage:  
' 1 - Set the top folder you want to start the search from in the variable strTopFldr
' 2 - Run the script
' 3 - Go get a coffee
' ************* Script Initialisation Statement Go Here ******************************
Option Explicit
Dim aryFile2Convrt()'Array Of Files to be Converted
Dim aryElem			'Elements in Array
Dim objExt			'Extension To Convert From
Dim objFSO			'File System Object
Dim strAryCount		'Count For Sizing And Increasing Array
Dim strDir			'Directory from search
Dim strExec			'Command line to run
Dim strFile			'Filename from search
Dim strInExt		'Input File Extension
Dim strNewDir		'New Directory for converted files
Dim strOutExt		'Output File Extension
Dim strTopFldr		'Top Folder To Search From
Dim wshShell		'Variable for execution section

Dim oExec
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
strAryCount = 0

' ************* User Deinable Variables *************
strTopFldr = "C:\ChangeMe"		'Top Directory Holding Files To Be Converted
strInExt = "wma"				'Format To Convert From
strOutExt = "mp3"				'Format to Convert To ***ONLY MP3 For Now***


' ************* Main Processing Section *********************************************
Call fnGetFolder(strTopFldr)

Call fnGoSubFolders(strTopFldr)
' ************* Subroutines and Functions Go Here ************************************

Function fnGetFolder(sFolder)											'Check Folder Exists And Can Be Used
	On Error Resume Next
	Set strTopFldr = objFSO.GetFolder(sFolder)
	If Err.Number <> 0 Then
		WScript.Echo "Error Connecting to Folder " & sFolder &vbLf &"[Error Number" & Err.Number &"] " &Err.Description
	Wscript.Quit Err.Number
	End If
	On Error Goto 0
End Function

Function fnGoSubFolders(objDir)											'Function To get folders.  Calls itself to go into subdirs
	If objDir <> "\System Volume Information" Then
		Call fnListFiles(objDir)
		For Each strDir In objDir.SubFolders
			Call fnGoSubFolders(strDir)
		Next
	End If
End Function

Function fnListFiles(objDir)
	For Each strFile In objDir.Files
		If LCase(objFSO.GetExtensionName(strFile)) = LCase(strInExt) Then	'Check for input files
			ReDim Preserve aryFile2Convrt(strAryCount)						'increase array size preserving existing data
			strFile = """"&objFSO.GetAbsolutePathName(strFile)&""""			'double quotes the full path & filename
			aryFile2Convrt(strAryCount) = strFile							'store the data in the array
			strAryCount = strAryCount + 1									'increment count ready for next file
		End If
		If strOutExt = "mp3" Then
			strExec = """C:\Program Files\NCH Swift Sound\Switch\switch.exe"" -clear -format MP3 -settings .mp3 CBR 96 TRUE STEREO TRUE -outfolder ""[Same as source file]"" -overwrite ""NEVER"" -convert"
			Else
			Wscript.Echo "Only MP3 Output Format Currently Supported"
			WScript.Quit
		End If
		For Each aryElem In aryFile2Convrt
			strExec = strExec&" "&aryElem										'Add Each Song to the command
		Next
		strExec = strExec&" -exit"											'Adds the exit command to the end of the command
		strExec = Replace (strExec, vblf, "")								'removes line feeds from the command
		strExec = Replace (strExec, vbcr, "")								'removes carriage returns from the command
	Next

	If strAryCount > 0 Then
		Set oExec = WshShell.Exec(strExec)
		Do While oExec.Status = 0
    		WScript.Sleep 100
		Loop
		ReDim aryFile2Convrt(0)
		strAryCount = 0	
	End If
End Function

