Page 1 of 1

Problems with taskkill /PID

Posted: 29 Jan 2020 12:51
by alpha4701
Hey there!
I am just starting with batch and i am having a really hard time with my program. I want it to open a specific website in firefox in a new window. After some time this and only this window(so other Firefox Windows can run in the background) should close again. My program works when there is no other Firefox Window running but when another window is running my program doesn't work anymore. It opens the Website but then it stops. Can somebody help me? Please :cry:

@echo off
tasklist /V>%temp%\vor.txt

start firefox.exe

tasklist /V>%temp%\nach.txt

for /F "tokens=2 delims= " %%i in ('fc %temp%\vor.txt %temp%\nach.txt^|find "firefox.exe"') do set PID=%%i

echo PID: %PID%

del %temp%\vor.txt
del %temp%\nach.txt

TASKKILL /PID %PID% /T /F

Re: Problems with taskkill /PID

Posted: 31 Jan 2020 10:51
by jfl
The list of tasks running in Windows is very long, and keeps changing all the time. Even if you don't start or stop anything yourself.
It'll be much more reliable to filter lists before comparing them.
Also, wait for a while before getting the second list, to give time to Firefox to start.

Code: Select all

@echo off
tasklist /V | find "firefox.exe" >%temp%\vor.txt

start firefox.exe

ping -n 2 localhost >NUL & :# Wait one second

tasklist /V | find "firefox.exe" >%temp%\nach.txt

for /F "tokens=2 delims= " %%i in ('fc %temp%\vor.txt %temp%\nach.txt ^| find "notepad.exe"') do set PID=%%i

...
Finally, I think the less information you get, the more reliable your code will be.
Instead of tasklist, consider using:

Code: Select all

wmic process get processid,executablepath