Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Vbscript -how to read in and use a config file

Status
Not open for further replies.

AndyDookie

Technical User
Feb 17, 2011
1
AU
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 = 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
'-------------------------------------------------------------------------------------------------
 
I would recommend combining the two sub - searchforapps() and terminateprogram() - into a single sub that accepts an argument. This would allow you to get rid of using global variables and, instead, use variables that are defined with the scope of the function.

Also, I may be mistaken but I do not believe an ExecQuery can contain double quotes. Replace them with single quotes.

I don't understand this line. In fact, I've never seen a variable defined with paranthesis encapsulation. I'm pretty sure this is one of the problems

strProcessKill = (" 'wmplayer.exe' ")
should be:
strProcessKill = "winplayer.exe"


Code:
sub killProcess (strComputer, strProcess)
   dim colProcesses
   dim objWMIService
   dim objProcess

   set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
   set colProcesses = objWMIService.ExecQuery([red]"Select * From Win32_Process Where Name = '" & strProcess & "'")[/red]

   for each objProcess in colProcess
      objProcess.Terminate()
   next
end sub

Usage:
strComputer = "."
strProcessKill = "winplayer.exe"
killProcess strComputer, strProcessKill

Hope this helps

-Geates

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live."
- Martin Golding
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top