I am trying to convert a batch file to VBS. This is what I have so far. Looking for advice on proper formatting, as well as any help as to why this VBS script isn't working at the moment. Here's what I've got so far:
OPTION EXPLICIT
DIM strComputer,strSCVA,strDOSBox
strComputer = "." ' local computer
strSCVA = "scva.exe"
strDOSBox = "dosbox.exe"
' Check if scva.exe is running at startup (. = local computer)
IF isProcessRunning(strComputer,strSCVA) THEN
Call LoopStart
ELSE
cd "%~dp0../"
start /min "" "scva.exe"
Call LoopStart
END IF
Sub LoopStart
' Exits script if scva.exe is manually terminated, otherwise, calls CheckDOSBox
IF isProcessRunning(strComputer,strSCVA) THEN
Call CheckDOSBox
ELSE
WScript.Quit
END IF
Sub CheckDOSBox
' Goes to LoopStart while dosbox.exe is running, otherwise, calls Kill
IF isProcessRunning(strComputer,strDOSBox) THEN
Call LoopStart
ELSE
Call Kill
END IF
Sub Kill
' Sends command to terminate scva.exe while scva.exe is running, otherwise, exits script
IF isProcessRunning(strComputer,strSCVA) THEN
taskkill /IM scva.exe
Call Kill
ELSE
WScript.Quit
END IF
' Function to check if a process is running
FUNCTION isProcessRunning(BYVAL strComputer,BYVAL strProcessName)
DIM objWMIService, strWMIQuery
strWMIQuery = "Select * from Win32_Process where name like '" & strProcessName & "'"
SET objWMIService = GETOBJECT("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
IF objWMIService.ExecQuery(strWMIQuery).Count > 0 THEN
isProcessRunning = TRUE
ELSE
isProcessRunning = FALSE
END IF
END FUNCTION
And here's the original batch file that's being converted (which I also wrote):
REM Runs scva.exe if it is not running; kills process after dosbox.exe is terminated
tasklist /FI "IMAGENAME eq scva.exe" 2>NUL | find /I /N "scva.exe">NUL
if "%ERRORLEVEL%"=="0" (
goto LoopStart
) else (
cd "%~dp0../"
start /min "" "scva.exe"
goto LoopStart
)
:LoopStart
REM Exits script if scva.exe is manually terminated, otherwise, goes to CheckDOSBox
tasklist /FI "IMAGENAME eq scva.exe" 2>NUL | find /I /N "scva.exe">NUL
if "%ERRORLEVEL%"=="0" (
goto CheckDOSBox
) else (
exit
)
:CheckDOSBox
REM Goes to LoopStart while dosbox.exe is running, otherwise, goes to Kill
tasklist /FI "IMAGENAME eq dosbox.exe" 2>NUL | find /I /N "dosbox.exe">NUL
if "%ERRORLEVEL%"=="0" (
goto LoopStart
) else (
goto Kill
)
:Kill
REM Sends command to terminate scva.exe while scva.exe is running, otherwise, exits script
tasklist /FI "IMAGENAME eq scva.exe" 2>NUL | find /I /N "scva.exe">NUL
if "%ERRORLEVEL%"=="0" (
taskkill /IM scva.exe
goto Kill
)
exit
I think that my problem area is not knowing how to properly start a program in VBS. I have googled this but I don't understand the implementation and it would help if someone could show me how it works in my own code. Additionally, what is the equivalent of cd "%~dp0../" in VBS? In other words, the equivalent to relatively setting the working directory to the current directory of the VBS script?