# vbscript - how to read in and use a config file



## AndyDookie

I do volunteer work for a community Radio Station and need some help with vbscript to read from a users editable config file into my script. Script is to load any music player stall for a determined time before running "ZaraRadio" an automated playout program. Zara needs the audio drivers loaded by any other player as it does not have drivers for our MAudio Delta 44 sound card.

My config file:
#Hash lines are comment lines
#You may add as many hash lines as you want
AMusicProg="""C:\Program Files\Windows Media Player\wmplayer.exe"""
ASongToPlay="""D:\My Documents\Andrew\LimeWire Files\Surfing Music\Surf Music ver2.mp3"""
SleepTime=5000
strProcessKill=("""wmplayer.exe""")
#End of the configuration file


My script so far. It fails to load the player
And I need a routine to kill the exact player in the config file as it may change for different studios.
Any help would be appreciated.
script:

Option Explicit
DIM FSO: SET FSO = CreateObject ("Scripting.FileSystemObject")
'Declare the variables to be used from the property file
DIM AMusicProg, ASongToPlay,SleepTime, strProcessKill
Dim objWMIService, objProcess, colProcess
Dim strComputer, WshShell 'dim and allocate ZaraProgTarget = """Somewhwere\zara.exe"""
strComputer = "."

SetConfigFromFile("D:\My Documents\Andrew\VBFiles\Practice\zaraconfig.txt") 'read in zaraconfig.txt
msgbox "AMusicProg=" & AMusicProg
'msgbox "ASongToPlay=" & AMusicProg
'msgbox "SleepTime=" & SleepTime
'msgbox "strProcessKill =" & strProcessKill
'AMusicProg=("""C:\Program Files\Windows Media Player\wmplayer.exe""") 'could be any player
'ASongToPlay = """D:\My Documents\Andrew\LimeWire Files\Surfing Music\Surf Music ver2.mp3""" 'any song can be used!

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run (AMusicProg) '& ASongToPlay, 4

'SleepTime = 5000
'Wscript.Sleep SleepTime 'fiddle sleep for time for Zara's drivers to load

strProcessKill = (" 'wmplayer.exe' ") '********** could be QuickTime?

' *************Insert Zara.exe here ***********************
'WshShell.Run ZaraProgTarget, 3

searchforapps 'cheack all running apps
terminateprogram 'kill strProcessKill = "'wmplayer.exe'" '********** could be QuickTime?

WSCript.Echo "Just killed process " & strProcessKill _
& " on " & strComputer

WSCript.Quit 'Empty ram of wscript

'---------------------------------Get Variables from ZaraConfig.txt------------------------
Sub SetConfigFromFile(fileName)
DIM strConfigLine,fConFile,EqualSignPosition, strLen, VariableName, VariableValue
SET fConFile = fspenTextFile(fileName)
WHILE NOT fConFile.AtEndOfStream
strConfigLine = fConFile.ReadLine
strConfigLine = TRIM(strConfigLine)
'msgbox(strConfigLine)
IF (INSTR(1,strConfigLine,"#",1) <> 1 AND LEN(strConfigLine) <> 0) THEN
EqualSignPosition = INSTR(1,strConfigLine,"=",1)
strLen = LEN(strConfigLine)
VariableName = LCASE(TRIM(MID(strConfigLine, 1, EqualSignPosition-1))) 'line 34
VariableValue = TRIM(Mid(strConfigLine, EqualSignPosition + 1, strLen - EqualSignPosition))
SELECT CASE VariableName
'ADD EACH OCCURRENCE OF THE CONFIGURATION FILE VARIABLES(KEYS)
CASE LCASE("AMusicProg")'
IF VariableValue <> "" THEN AMusicProg = VariableValue
CASE LCASE("ASongToPlay")'
IF VariableValue <> "" THEN ASongToPlay = VariableValue
CASE LCASE("SleepTime")'
IF VariableValue <> "" THEN SleepTime = VariableValue
CASE LCASE("strProcessKill")'
IF VariableValue <> "" THEN strProcessKill = VariableValue 
END SELECT
END IF
WEND
fConFile.Close
End Sub '------------------------------------------------------------------------

'--------------------------cheack all running apps----------------------------------------
sub searchforapps()
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
end sub
'------------------------------kill strProcessKill from all running apps=** could be QuickTime?----------------------
sub terminateprogram()
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
objProcess.Terminate()
Next
end sub
'--------------------------------------------------------------------------


----------



## TheOutcaste

Moving this to the Programming Forum.

When reading the variables from a file, you don't need the three double quotes, that's only needed when defining a variable that needs to contain a quote.

So use this for the Configuration file (you can use the %ProgramFiles% variable):


Code:


#Hash lines are comment lines
#You may add as many hash lines as you want
AMusicProg="%ProgramFiles%\Windows Media Player\wmplayer.exe"
ASongToPlay="E:\Music\Alan Parsons\Try Anything Once\02 - Turn It Up.mp3"
SleepTime=5000
strProcessKill='wmplayer.exe'
#End of the configuration file

The 2nd msgbox in your script has the wrong variable.
I added some lines to get the actual Computer name. I don't have the zara program to test with, but maybe this will get you to that point at least.


Code:


Option Explicit
DIM FSO: SET FSO = CreateObject ("Scripting.FileSystemObject")
'Declare the variables to be used from the property file
DIM AMusicProg, ASongToPlay,SleepTime, strProcessKill
Dim objWMIService, objProcess, colProcess, obNetwork
Dim strComputer, WshShell,strComputerName
'dim and allocate ZaraProgTarget = """Somewhwere\zara.exe"""

strComputer = "."

Set obNetwork = Wscript.CreateObject("Wscript.Network" )
strComputerName = obNetwork.ComputerName
Set obNetwork = nothing

SetConfigFromFile("C:\Temp Dir\vbtest\zaraconfig.txt") 'read in zaraconfig.txt
msgbox "AMusicProg=" & AMusicProg
'msgbox "ASongToPlay=" & ASongToPlay
'msgbox "SleepTime=" & SleepTime
'msgbox "strProcessKill =" & strProcessKill

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run AMusicProg & " " & ASongToPlay, 4
SleepTime = 10000
Wscript.Sleep SleepTime 'fiddle sleep for time for Zara's drivers to load

' *************Insert Zara.exe here ***********************
'WshShell.Run ZaraProgTarget, 3

searchforapps    'cheack all running apps
terminateprogram 'kill strProcessKill = "'wmplayer.exe'" '********** could be QuickTime?

WScript.Echo "Just killed process " & strProcessKill & " on " & strComputerName

WScript.Quit     'Empty ram of wscript

'---------------------------------Get Variables from ZaraConfig.txt------------------------
Sub SetConfigFromFile(fileName)
DIM strConfigLine,fConFile,EqualSignPosition, strLen, VariableName, VariableValue
SET fConFile = fso.OpenTextFile(fileName)
WHILE NOT fConFile.AtEndOfStream
  strConfigLine = fConFile.ReadLine
  strConfigLine = TRIM(strConfigLine)
  'msgbox(strConfigLine)
  IF (INSTR(1,strConfigLine,"#",1) <> 1 AND LEN(strConfigLine) <> 0) THEN
    EqualSignPosition = INSTR(1,strConfigLine,"=",1)
    strLen = LEN(strConfigLine)
    VariableName = LCASE(TRIM(MID(strConfigLine, 1, EqualSignPosition-1))) 'line 34
    VariableValue = TRIM(Mid(strConfigLine, EqualSignPosition + 1, strLen - EqualSignPosition))
    SELECT CASE VariableName
      'ADD EACH OCCURRENCE OF THE CONFIGURATION FILE VARIABLES(KEYS)
      CASE LCASE("AMusicProg")
        IF VariableValue <> "" THEN AMusicProg = VariableValue
      CASE LCASE("ASongToPlay")
        IF VariableValue <> "" THEN ASongToPlay = VariableValue
      CASE LCASE("SleepTime")
        IF VariableValue <> "" THEN SleepTime = VariableValue
      CASE LCASE("strProcessKill")
        IF VariableValue <> "" THEN strProcessKill = VariableValue
    END SELECT
  END IF
WEND
fConFile.Close
End Sub '------------------------------------------------------------------------

'--------------------------cheack all running apps----------------------------------------
sub searchforapps()
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
end sub
'------------------------------kill strProcessKill from all running apps=** could be QuickTime?----------------------
sub terminateprogram()
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = " & strProcessKill )
For Each objProcess in colProcess
  objProcess.Terminate()
Next
end sub
'--------------------------------------------------------------------------


----------



## AndyDookie

Thanks that did the trick I had forgotten the %% variable usage. Works fine here at home
Seems to work ok now. I have to edit the ZaraRadio.exe call now when I get to the Radio Station and use the local paths etc. I will let you know how it goes!
Andrew


----------

