gpt4 book ai didi

batch-file - 意外使用 ( 批处理文件

转载 作者:行者123 更新时间:2023-12-01 22:26:46 26 4
gpt4 key购买 nike

在下面的代码中,如果发现 ABClient.exeABClientMonitor.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
)
)

最佳答案

在括号内声明的变量需要延迟扩展调用,否则它们实际上不存在。在这种情况下,由于 %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
)
)

关于batch-file - 意外使用 ( 批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33250755/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com