我想将批处理文件转换为VBS。这是我迄今为止所拥有的。寻找适当格式的建议,以及有关此VBS脚本目前无法正常工作的任何帮助。下面是我到目前为止有:需要帮助获取此批处理VBS转换才能正常工作
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
这里是一个的被转换原来的批处理文件(这也是我写的):
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
我认为我的问题领域是不知道如何在VBS中正确启动一个程序。我用google搜索了这个,但是我不明白这个实现,如果有人能够告诉我它是如何在我自己的代码中工作的话,这将会有所帮助。另外,VBS中cd“%〜dp0 ../”的等效值是多少?换句话说,相当于将工作目录相对设置为VBS脚本的当前目录?
我认为这可能是我在这里看到的第一个“我该如何转换这个代码”的问题,实际上就是_good_。 – SomethingDark
另外,我看到两个子声明,但没有'end sub's。无可否认,我唯一的VB *体验是使用VBA,但我确定你需要这些。 – SomethingDark