我想將批處理文件轉換爲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