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?
Taskkill %0
Moderator: DosItHelp
Re: Taskkill %0
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.
Steffen
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
)
Re: Taskkill %0
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.
-
- Posts: 3
- Joined: 26 Apr 2021 07:31
Re: Taskkill %0
The batch is not scheduled with Windows Task Scheduler but still thanks for your reply.
-
- Posts: 3
- Joined: 26 Apr 2021 07:31
Re: Taskkill %0
Wow thank you. This works like a charm.aGerman wrote: ↑26 Apr 2021 10:54It'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.SteffenCode: 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 )
Re: Taskkill %0
Hi
You can also use the WMIC to "kill your self" oh no I mean killing your batch file
You can also use the WMIC to "kill your self" oh no I mean killing your batch file
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
Re: Taskkill %0
Untested, but have you tried including the wildcard within the doublequoted filter itself?Arne Schönbohm wrote: ↑26 Apr 2021 07:52TASKKILL /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?
Code: Select all
%SystemRoot%\System32\taskkill.exe /F /Fi "WindowTitle Eq %0*"
Code: Select all
%SystemRoot%\System32\taskkill.exe /F /Fi "ImageName Eq cmd.exe" /Fi "WindowTitle Eq %0*"
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