问题描述
我正在尝试制作一个批处理脚本来在启动时终止 LGHub(LGHub 需要加载才能启动运行我鼠标宏/绑定的服务,但我不需要完整的程序/窗口这些服务启动后在我的桌面上打开)。根据我发现在启动时启动批处理文件的说明,我创建了文件的快捷方式并将快捷方式扔到了 Windows Startup
文件夹中。
现在,我以前从未编写过批处理脚本,但是根据我的研究和从其他线程中获取的代码,这应该可以完成工作。如果没有,那么,A) 是这样,但是 B) 文件似乎在启动时启动,但随后立即关闭,而不是像它应该的那样等待。
我当前的脚本是
@ECHO off
set /A counter=0
:while
IF %counter% le 30 (
tasklist /FI "IMAGENAME eq lghub.exe" 2>NUL | find /I /N "lghub.exe">NUL
IF "%ERRORLEVEL%"=="0" (
taskkill /im "lghub.exe"&exit
) ELSE (
set /A counter+=1
SLEEP 1
GOTO :while
)
)
echo "Could not kill LG Hub after 30 seconds."&exit
解决方法
@echo off
cd /d "%~dp0" & set "_cnt=0"
title <nul && title ..\%~nx0
Setlocal EnableDelayEdexpansion
:while
call set /a "_cnt+=1" & cls & echo\ & if !_cnt! leq 29 (
2>nul tasklist /nh | 2>nul find /i "lghub.exe" >nul || (
timeout 1 /nobreak | echo\ Trying to kill LG Hub: !_cnt! & goto:while )
)
echo "Could not kill LG Hub after !_cnt! seconds." & endlocal
为什么不尝试将 le
替换为 leq
,并使用 Setlocal EnableDelayEdexpansion
IF %counter% le 30 (...