我正在努力讓這個東西發揮作用,但出於某種原因,即使我認為它是正確的,它似乎並沒有遵守 else 語句。結果,就像沒有其他,它只是執行所有程式碼行。目的是檢查某些東西正在運行。如果是,那麼它應該顯示一條訊息並使命令視窗保持開啟狀態,以便人們可以看到需要執行哪些操作來修復它,然後當按下某個鍵時,命令視窗將關閉。如果它沒有運行,它將為他們啟動它並關閉命令窗口,這樣人們就看不到它了。
以下是顯示正在發生的情況的輸出(刪除了空白行以節省空間):
C:\Users\me2\Desktop>QPROCESS "Pulse.exe" 1>NUL No Process exists for pulse.exe
C:\Users\me2\Desktop>REM 0 means that it's running (ProcessFound) and 1 mean s it's not (ProcessNotFound)
C:\Users\me2\Desktop>REM cmd /k IF (1 EQU 0 goto ProcessFound) ELSE (goto Pr ocessNotFound)
C:\Users\me2\Desktop>REM cmd /k IF 1 NEQ 0 (
C:\Users\me2\Desktop>cmd /k IF ERRORLEVEL 1 (
C:\Users\me2\Desktop>echo In ProcessNotFound In ProcessNotFound
C:\Users\me2\Desktop>REM cd C:\Program Files (x86)\Common Files\Pulse Secure \JamUI\
C:\Users\me2\Desktop>REM start "" Pulse.exe -tray
C:\Users\me2\Desktop>echo In ProcessFound In ProcessFound
C:\Users\me27\Desktop>REM cls 0
Pulse Secure is running. It must be stopped by right clicking on the icon and clicking Exit.
Press any key to continue . . .
這是代碼:
QPROCESS "Pulse.exe">NUL
REM 0 means that it's running (ProcessFound) and 1 means it's not (ProcessNotFound)
REM cmd /k IF (%ERRORLEVEL% EQU 0 goto ProcessFound) ELSE (goto ProcessNotFound)
REM cmd /k IF %ERRORLEVEL% NEQ 0 (
cmd /k IF ERRORLEVEL 1 (
:ProcessNotFound
echo In ProcessNotFound
REM cd C:\Program Files (x86)\Common Files\Pulse Secure\JamUI\
REM start "" Pulse.exe -tray
) ELSE (
:ProcessFound
echo In ProcessFound
REM cls
@echo off
echo %ERRORLEVEL%
ECHO.
ECHO.
ECHO Pulse Secure is running.
ECHO It must be stopped by right clicking on the icon and clicking Exit.
ECHO.
ECHO.
PAUSE
)
REM IF %ERRORLEVEL% EQU 0 ECHO "Pulse Secure is running. It must be stopped by right clicking on the icon and clicking Exit."
答案1
- 您
cmd /k
將在同一視窗中產生 cmd.exe 的新實例。 - 這個新的cmd.exe中的錯誤等級指的是cmd.exe本身的啟動,這顯然曾是成功,所以錯誤等級為0
- 只需刪除所有不必要的
cmd /k
- 您也可以檢查
%errorlevel%
,這並不意味著if errorlevel x or greater
您的語法的作用。
在不知道 QPROCESS 的情況下,您可以在失敗||
或成功時使用條件執行&&
,請參閱https://ss64.com/nt/syntax-conditional.html
QPROCESS "Pulse.exe" >NUL 2>&1 || (
echo In ProcessNotFound
REM cd C:\Program Files (x86)\Common Files\Pulse Secure\JamUI\
REM start "" Pulse.exe -tray
) && (
echo In ProcessFound
echo %ERRORLEVEL%
ECHO.
ECHO Pulse Secure is running.
ECHO It must be stopped by right clicking on the icon and clicking Exit.
ECHO.
PAUSE
)