在以下代码中,如果发现正在运行,我想终止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
)
)
答案 0 :(得分:6)
在括号内声明的变量需要通过延迟扩展来调用,否则它们实际上不存在。在这种情况下,由于%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
)
)