Problems with taskkill /PID

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 1
Joined: 29 Jan 2020 12:42

Problems with taskkill /PID

#1 Post by alpha4701 » 29 Jan 2020 12:51

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


Posts: 140
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France

Re: Problems with taskkill /PID

#2 Post by jfl » 31 Jan 2020 10:51

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

Post Reply