Taskkill %0

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Arne Schönbohm
Posts: 3
Joined: 26 Apr 2021 07:31

Taskkill %0

#1 Post by Arne Schönbohm » 26 Apr 2021 07:52

Hello,

I have a long batch that runs for some hours. To see how far it is the Title changes:

TITLE %0
...
TITLE %0 - file1
CALL C:\Path\file1
TITLE %0 - file2
CALL C:\Path\file2
TITLE %0 - file3
CALL C:\Path\file3
...

It can happen that it runs very long. Since this runs every 24h I would like to close the older turn if the next turn starts.

TASKKILL /F /FI "WINDOWTITLE EQ "%0"" would work if the i would not add "- file1" and so on. But if I add * to this command it doesn't work:

TASKKILL /F /FI "WINDOWTITLE EQ "%0" *"

Where is my mistake or does it not work this way?

aGerman
Expert
Posts: 4186
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Taskkill %0

#2 Post by aGerman » 26 Apr 2021 10:54

It's a little problematic due to the quotes around the own path of the script. I'd probably use a filtered output of tasklist to get the PID. As long as the title still doesn't contain the " - " you can use this snippet in the same script that later on calls file1-N.

Code: Select all

set me=%0
set me=%me:\=\\%
for /f tokens^=3^ delims^=^" %%i in ('"tasklist /v /nh /fi "imagename eq cmd.exe" /fo csv|findstr /c:"%me:"=\"% - ""') do (
  taskkill /f /pid %%i
)
Steffen

Squashman
Expert
Posts: 4292
Joined: 23 Dec 2011 13:59

Re: Taskkill %0

#3 Post by Squashman » 26 Apr 2021 10:56

If you are scheduling this to run with Windows Task Scheduler it has this functionality built-in to the task creation. You just need edit the appropriate settings in the scheduled task.

Arne Schönbohm
Posts: 3
Joined: 26 Apr 2021 07:31

Re: Taskkill %0

#4 Post by Arne Schönbohm » 27 Apr 2021 03:14

Squashman wrote:
26 Apr 2021 10:56
If you are scheduling this to run with Windows Task Scheduler it has this functionality built-in to the task creation. You just need edit the appropriate settings in the scheduled task.
The batch is not scheduled with Windows Task Scheduler but still thanks for your reply.

Arne Schönbohm
Posts: 3
Joined: 26 Apr 2021 07:31

Re: Taskkill %0

#5 Post by Arne Schönbohm » 27 Apr 2021 03:15

aGerman wrote:
26 Apr 2021 10:54
It's a little problematic due to the quotes around the own path of the script. I'd probably use a filtered output of tasklist to get the PID. As long as the title still doesn't contain the " - " you can use this snippet in the same script that later on calls file1-N.

Code: Select all

set me=%0
set me=%me:\=\\%
for /f tokens^=3^ delims^=^" %%i in ('"tasklist /v /nh /fi "imagename eq cmd.exe" /fo csv|findstr /c:"%me:"=\"% - ""') do (
  taskkill /f /pid %%i
)
Steffen
Wow thank you. This works like a charm.

Hackoo
Posts: 92
Joined: 15 Apr 2014 17:59

Re: Taskkill %0

#6 Post by Hackoo » 27 Apr 2021 06:14

Hi :wink:
You can also use the WMIC to "kill your self" oh no :D :lol: I mean killing your batch file :lol:

Code: Select all

@echo off
Title BATCH SELF KILLING
:MAIN
CLS
Set "ME=%~0"
Set "ME=%ME:\=\\%"
REM Just To show what can be the result
wmic PROCESS where "CommandLine Like '%%%ME%%%' And Name Like '%%cmd.exe%%'" get ProcessID,Name,CommandLine
::----------------------------------------------------------------------------------------------------------------------------------------------
@for /f "Tokens=2 Delims==" %%a in ('wmic PROCESS where "CommandLine Like '%%%ME%%%' And Name Like '%%cmd.exe%%'" get ProcessID /Value') do ( 
	@for /f "delims=" %%b in ("%%a") do (
		set "MyPID=%%~nb"
	)
)
::----------------------------------------------------------------------------------------------------------------------------------------------
echo MyPID = "%MyPID%"
echo Do you want to Kill your Self ? oh No Sorry (-_°) I mean killing the current batch file [Y=YES]
Set /P "Answer="
IF /I [%Answer%] EQU [Y] Taskkill /f /pid %MyPID%
) Else (
	echo Thanks to God ... We are still alive hhhhh 
	TimeOut /T 3 /Nobreak>nul
	IPconfig /ALL
)
pause>nul & GOTO Main

Compo
Posts: 538
Joined: 21 Mar 2014 08:50

Re: Taskkill %0

#7 Post by Compo » 27 Apr 2021 10:10

Arne Schönbohm wrote:
26 Apr 2021 07:52
TASKKILL /F /FI "WINDOWTITLE EQ "%0"" would work if the i would not add "- file1" and so on. But if I add * to this command it doesn't work:

TASKKILL /F /FI "WINDOWTITLE EQ "%0" *"

Where is my mistake or does it not work this way?
Untested, but have you tried including the wildcard within the doublequoted filter itself?

Code: Select all

%SystemRoot%\System32\taskkill.exe /F /Fi "WindowTitle Eq %0*"
It may also be quicker to filter the image name first too:

Code: Select all

%SystemRoot%\System32\taskkill.exe /F /Fi "ImageName Eq cmd.exe" /Fi "WindowTitle Eq %0*"
I will add however, that '%0' used like that is probably not a wise idea if the batch file name it represents contains spaces or other poison characters, as it could itself be doublequoted. That would obviously case an issue with the doublequotes in your filter. For that reason it would be better were you to not use it like that in your title. What about using '%~nx0' or '%~s0', if you really need that in the title, although it may be better to just use a known safe string instead.

I do not however recommend that you use the '/F' option to force close your task, allow it to close properly, and omit the '/F' option

Post Reply