Dim strMyFullName, strMyPath, objArgs, i, strCmd, patt1, patt2
Dim WshShell, Pipe, origName, newName, atATime, nbrToCompressAtOnce

' Set the following to the number of compression tasks you want to run at once
nbrToCompressAtOnce = 4

' The following renames the file to the standard naming convention at the time of compression
Set re1 = New RegExp
re1.Pattern = "(SKRM|MVE) ([^ ]{3})[^ ]* Ch ([\d]*).mp3"
patt1="$20$3.mp3"
Set re2 = New RegExp
re2.Pattern = "Luke Chapter ([\d]*).mp3"
patt2="Luk0$1.mp3"

strMyFullName = WScript.ScriptFullName
lastBackSlash = InStrRev(strMyFullName, "\")
strMyPath = Mid(strMyFullName, 1, lastBackSlash)

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory = strMyPath
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments

atATime = nbrToCompressAtOnce

for i = 0 to objArgs.Count - 1
	origName = objArgs(i)
	newName = origName
	lastBackSlash = InStrRev(newName, "\")
	newFolderName = Mid(newName, 1, lastBackSlash) & "Compressed"
	If not FSO.FolderExists(newFolderName ) Then
		FSO.CreateFolder newFolderName
	End If
	newFileName = Mid(newName, lastBackSlash)
	'~ WScript.Echo "newFileName=" & newFileName
	
	If re1.test(newFileName) Then
		newFileName=re1.replace(newFileName,patt1)
	End If
	If re2.test(newFileName) Then
		newFileName=re2.replace(newFileName,patt2)
	End If
	
	'~ WScript.Echo "newFileName=" & newFileName
	newName = newFolderName & newFileName
	
	' The following defines the path of the "lame" program used for compression and the options.
	' I've found these to work well for speech, producing a small file without reducing the quality excessively.
	' -q0                          - the highest quality, given the other parameters below
	' -a                             - make mono instead of stereo
	' -cbr                         - use constant bit rate
	' -b 24                       - 24 bits per sample
	' --resample 22.05 - use 22050 sample rate per second
	' --priority 4            - highest CPU priority - set this down if you want Lame to run with less impact in the background
	strCmd = """C:\\Program Files (x86)\\Lame For Audacity\\lame"" -q0 -a -cbr -b 24 --resample 22.05 --priority 4 """ & origName & """ """ & newName & """"

	atATime = atATime - 1
	if (atATime <= 0) Then
		WshShell.Run strCmd, ,True ' wait for Lame to finish before beginning the next compression task
		atATime = nbrToCompressAtOnce
	Else
		WshShell.Run strCmd ' don't wait for Lame to finish
	End If
next
WScript.Quit
