在下面的代码中我想终止ABClient.exe,ABClientMonitor.exe如果发现它正在运行.但是,当尝试运行代码时,我收到Unexpected use of (错误
码:
@echo off
color 0b
:loop
tasklist | find /i "ABClient.exe" > nul
set processFound1=%errorlevel%
tasklist | find /i "ABClientMonitor.exe" > nul
set processFound2=%errorlevel%
if %processFound1% == 0 (
echo ABClient has been detected. Terminating...
taskkill /f /im "ABClient.exe" > nul
set process1lvl=%errorlevel%
if %process1lvl% == 0 (
echo ABClient has been terminated successfully!
goto loop2
) ELSE (
echo Failed to terminate ABClient!
goto loop2
)
)
:loop2
if %processFound2% == 0 (
echo ABClientMonitor has been detected. Terminating...
taskkill /f /im "ABClientMonitor.exe" > nul
set process2lvl=%errorlevel%
if %process2lvl% == 0 (
echo ABClientMonitor has been terminated successfully!
goto loop
) ELSE (
echo Failed to terminate ABClientMonitor!
goto loop
)
)
Run Code Online (Sandbox Code Playgroud)
在括号内声明的变量需要通过延迟扩展来调用,否则它们实际上不存在.在这种情况下,由于%process1lvl%和%process2lvl%变量的位置,内部if语句的计算结果为if == 0 (,这会导致语法错误.
要更正此问题,请将该行添加setlocal enabledelayedexpansion到脚本的开头,然后替换%process1lvl%为!process1lvl!并替换%process2lvl%为!process2lvl!.
@echo off
setlocal enabledelayedexpansion
color 0b
:loop
tasklist | find /i "ABClient.exe" > nul
set processFound1=%errorlevel%
tasklist | find /i "ABClientMonitor.exe" > nul
set processFound2=%errorlevel%
if %processFound1% == 0 (
echo ABClient has been detected. Terminating...
taskkill /f /im "ABClient.exe" > nul
set process1lvl=!errorlevel!
if !process1lvl! == 0 (
echo ABClient has been terminated successfully!
goto loop2
) ELSE (
echo Failed to terminate ABClient!
goto loop2
)
)
:loop2
if %processFound2% == 0 (
echo ABClientMonitor has been detected. Terminating...
taskkill /f /im "ABClientMonitor.exe" > nul
set process2lvl=!errorlevel!
if !process2lvl! == 0 (
echo ABClientMonitor has been terminated successfully!
goto loop
) ELSE (
echo Failed to terminate ABClientMonitor!
goto loop
)
)
Run Code Online (Sandbox Code Playgroud)