how to kill all batch files except the one currently running - windows

How can you run a batch file which taskkills all other cmd.exes which are currently running, except for the one that is doing the task kill command?

Combining these 2 threads:
how to get PID from command line filtered by username and imagename
how to get own process pid from the command prompt in windows
you can write down something like this in a simple cmd file (akillfile.cmd)
title=dontkillme
FOR /F "tokens=2 delims= " %%A IN ('TASKLIST /FI ^"WINDOWTITLE eq dontkillme^" /NH') DO SET tid=%%A
echo %tid%
taskkill /F /IM cmd.exe /FI ^"PID ne %tid%^"

copy cmd.exe, rename it to a.exe, then use this command in a batch file: start a.exe /k taskkill /f /im cmd.exe

This worked for me, added to the very beginning of the .bat which is to kill all previously launched instances of itself, then immediately make itself susceptible to be killed by subsequent calls of the same .bat file:
title NewlyLaunchedThing
taskkill /F /IM cmd.exe /FI "WINDOWTITLE ne NewlyLaunchedThing"
title Thing
...do everything else
Note that, apparently, the "title" is a keyword, which sets a variable named "WINDOWTITLE".
Also, the "=" (equals sign) is apparently optional for assigning title/WINDOWTITLE. Meaning, this works as well, and may be preferred for clarity:
title = newTitle

Related

what should be the program or script for task scheduler

Through cmd, this command works fine and my service ABC gets terminate and starts,
taskkill /f /fi "memusage gt 100" /im ABC.exe && NET START ABC
Now I want to create a task scheduler to run this script every day,
In Add Arguments, I put the command.
if I am putting CMD in program\script, then it's not working, what shall I put there?
or do we have other way to get it done?
You should add /C to the cmd.exe arguments followed by the command you want to execute.
This should work:
Program/script:
cmd.exe
Arguments:
/S /C "taskkill /f /fi "memusage gt 100" /im ABC.exe && NET START PeerDistSvc"
Where flags from cmd /?:
/C Carries out the command specified by string and then
terminates
/S Modifies the treatment of string after /C or /K
...
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic
is used to process quote (") characters:
If ...:
no /S switch
...
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
alternatively put your command into a script file say "mycommand.cmd"
MyCommand.cmd:
taskkill /f /fi "memusage gt 100" /im ABC.exe && NET START ABC
then just reference this CMD /BAT file in the action of the task scheduler.
This is the easiest option when you have anything complex or will want to modify the code in any way on a regular basis because modifying the code in the scheduled task itself will always require entering your credentials to edit it.
In addition if you have a separate script file you can manually run it by hand as needed without opening the scheduler and running the task on demand.

how to kill a cmd process without killing others cmds?

I'm working on a batch file that is supposed to START a process (CMD) and then it should kill the process after finished. Problem is, that Imagename is cmd.exe and the other problem is that it should be running on Jenkins.
This is what I have tested:
Getting PID with wmic using name of window to find process -> Failed at Jenkins
Taskkill by naming the window-> Failed because Jenkins does not
display windows due to security issues.
Taskkill by imagename -> Failed because there are other cmd processes
running at the same time
Taskkill with pid but pid from the last cmd started. -Works but it is
not very safe.
I couldnĀ“t understand how wmic works but as I see, I cannot start a process with a command like with START command.
Conditions:
It can't be kill after some time because I need the output from the
mergetool and sometimes mergetool can take too long.
It should run at same time with other (cmd) processes // Jenkins
My question, is there a way of getting the PID from the START Command?
Here are some questions that helped me a lot!
Windows batch file : PID of last process?
Compare number of a specific process to a number
CODE:
set "console_name=cmd.exe"
set "git_command=%gitcmd% mergetool ^>output.txt"
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-before.txt
START "mergetool_w" CMD /c %git_command%
tasklist /FI "imagename eq %console_name%" /NH /FO csv > task-after.txt
for /f "delims=, tokens=2,*" %%A in ('fc /L /LB1 task-before.txt task-after.txt') do set pid=%%A
pid=!pid:"=!
echo pid is %pid%
TASKKILL /t /pid %pid% /f
You could actually use findstr for checking what tasks have been added after your start command line, relying on your files task-before.txt and task-after.txt:
findstr /LXVG:task-before.txt task-after.txt
Due to a nasty bug, this might under some circumstances lead to an unexpected output. To prevent that, add the /I option, if you can live with case-insensitive searches:
findstr /ILXVG:task-before.txt task-after.txt
Yes it's very possible. I'm going to take code from my previous awnser on another post here: Stop Execution of Batch File after 20 Seconds and move to Next
I want to first assume "mergetool_w" is the name of the CMD you are opining with the start...
The way you want to go about this is to search the tasklist for your console title and extract the PID# out of the context. The find suffix can be used to "Filter" the results along with tokens=2 to extract only the PID#.
FOR /F "tokens=2" %%# in ('tasklist /v ^| find "mergetool_w"') do set PID=%%#
From there, you can now kill this new window using the taskkill /pid command. The PID# is stored in the string %PID% so the command is simple:
taskkill /pid %PID% /t /f
Finaly, it looks as if you are trying to "Log" the data so feel free to put > text-task.txt where it's needed.

CMD command not running in batch file

I am trying to create a batch file that runs a command.
The first part of the command checks to see if a particularly named window is open on my desktop. If it is it then closes that window (by sending a command to the program with the window open - Virtual Audio Cable).
#For /f "Delims=:" %A in ('tasklist /nh /v /fi "WINDOWTITLE eq Headphones"') do #if %A==INFO (echo Prog not running) else (start /min "audiorepeater_ks" "c:program files\Virtual Audio Cable\audiorepeater_ks.exe" /CloseInstance: Headphones)
Now this command seems to work fine when I execute it via the command line.
But it does nothing when I try to put it in a batch file
I have tried a .bat file and a .cmd file and also created a shortcut to the .cmd file where i have prepended the target field in properties with "C:\Windows\System32\cmd.exe /c"
Any ideas on how I can get this to run via batch?
You will have to escape "%A" with "%%A" as CristiFati pointed out. For more escape characters, please visit https://www.robvanderwoude.com/escapechars.php

Cannot kill a process using the window title

I have two batch file one is always running (listerner.bat) but it is visible,the other one (mystop.bat)is to kill or stop my listener.bat and this two batch file are resides in this path C:\mydemo\mybatchfiles\,I am using windows 7
here is the code for mystop.bat
taskkill /F /FI "WINDOWTITLE eq Administrator: testlistener" /T
but when I run it,it will not terminate the running (listener.bat),There is no error but I have this message when I run it.
INFO: No tasks running with the specified criteria.
I appreciate someone can help me on this.I am new on this batch file command.
I had the same problem. In my case it was that there were two spaces in the window title:
taskkill.exe /F /FI "WindowTitle eq Administrator: TailingErrorLog"
^^
Taskkill considers the command currently executed by listener.bat as part of the title. So you need to add a wildcard "*".
taskkill /F /FI "WINDOWTITLE eq Administrator: testlistener *" /T
Try terminating using Im switch ..
Taskkill /Im listener.bat /t /f
Although this is not computer science related question , thought I should help ...

Batch script to close all open Command Prompt windows

I have a .cmd file which I call to open multiple instances of Command Prompt via:
launcher.cmd -fs
launcher.cmd -tds
launcher.cmd -fsd
Each command open a new command prompt.
So what I want to do is create a batch file to automatically close all the opened Command Prompt instead of manually doing it.
Be carefull: you might kill more processes than you want:
taskkill /IM cmd.exe
You can add extra filters:
taskkill /IM cmd.exe /FI "WINDOWTITLE eq launcher*"
use
tasklist /FI "imagename eq cmd.exe " /V
to get a glimpse of what cmd.exe processes will be taskkill-ed
You could add the /F parameter to force the process to close but I would only use that if the process doesn't respond to a normal request.
Just a little note why accepted answer from Rene may not work. I was starting my apps from cmd file like
start "" my.exe -my -args
where my.exe was a console app and it was looking like cmd window I wanted to kill, but process name was not cmd.exe (!) and I had to use command like
taskkill /IM my.exe
So in some cases it worth to check the real process name, for example in the windows task manager.
TASKKILL /F /IM cmd.exe /T
good solution

Resources