batch file - BAT Close Program if another is running -
i wanted bat program can use task scheduler if open "java.exe", close itself. possible?
thanks!
since findstr
returns 0 if string exists in input, , non-zero otherwise, can test on errorlevel
of command know whether program running or not.
tasklist | findstr /i "java.exe" > nul if errorlevel 1 exit /b 1 echo ...do here ...
you can compact in single line ||
boolean operator.
tasklist | findstr /i "java.exe" >nul || exit /b 1 echo ...do here ...
run help findstr
see list of options can give. if want more precise matches such "java.exe" not match "notjava.exe", can use /b
switch.
tasklist | findstr /i /b "java.exe" >nul || exit /b 1 echo ... here ...
the tasklist
program can own filtering, still have run output through findstr
because reason tasklist
seems set errorlevel
0. output changes depending on computer's localization settings, too, it's not flexible , generic of solution. here is:
tasklist /fi "imagename eq iexplore.exe" | findstr "iexplore.exe" >nul && echo task running
if want batch file stop process running, first you'd 1 of above options see whether running, , you'd run taskkill
stop it. e.g.,
taskkill /im /t "java.exe"
see taskkill /?
more options. depending on use case, might go directly taskkill
filter options, this:
taskkill /fi "imagename eq java.exe"
Comments
Post a Comment