0

I'm trying to make a batch script to kill LGHub after it launches on startup (LGHub needs to load in order to start services that run my mouse's macros/binds, but I don't need the full program/window sitting open on my desktop after those services launch). According to directions I found to launch the batch file at startup, I made a shortcut to the file and threw the shortcut in the Windows Startup folder.

Now, I've never written a batch script before, but from my research and the code taken from other threads, this should do the job. If not, well, A) there's that, but B) the file seems to launch on startup, but then close immediately, instead of waiting like it should.

My current script is

@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
PaulBunion
  • 346
  • 2
  • 18

1 Answers1

0
@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

Why not try to replace le to leq, and use Setlocal EnableDelayEdexpansion

IF %counter% le 30 (...
Io-oI
  • 2,514
  • 3
  • 22
  • 29