Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Kill all but last process

Reply
Thread Tools Display Modes

Re: Kill all but last process

 
 
Al Dunbar
Guest
Posts: n/a

 
      09-02-2008
On my system, when I have multiple instances of word running, I only see a
single task, using either task manager or tasklist. Your script below would
find and delete the only word task in existence, killing all visible
instances.

Your approach would work for Notepad, for which each instance is a separate
task. You'd just need to have one FOR loop to count them all, and another to
delete all but one. The question is - which is the "last" one?

To close all but one specific word window would require a different
technique, but that is an area I am unfamiliar with.


/Al

"oztech" <> wrote in message
news...
>
> I have a script which kills the Microsoft Word instances running on my
> Windows 2003 server. The problem is it kills all the instances, where as
> I want to keep the last instance or last microsoft word process as it is
> And kill the rest of the ones.
> Here is the code:
>
> FOR /F "tokens=2" %%I IN ('tasklist ^| Find "WINWORD.EXE"') DO CALL
> :windowlist %%I
> goto :EOF
> goto :EOF
> :windowlist
> echo %1
> TASKKILL /PID %1 /F
> goto :EOF
>
> How can I achieve this?
>
>
> --
> oztech
> ------------------------------------------------------------------------
> oztech's Profile: http://forums.techarena.in/members/oztech.htm
> View this thread: http://forums.techarena.in/server-scripting/1030312.htm
>
> http://forums.techarena.in
>



 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      09-02-2008

"oztech" <> wrote in message
news...
>
> These word instances are started via automation, and also if you run
> winword /a then you will get multiple word instances.
> Tasklist will give you a list of process, in the order of started first
> to started last. So when I hit the last process in that I want to stop.
>
>
> --
> oztech
> ------------------------------------------------------------------------
> oztech's Profile: http://forums.techarena.in/members/oztech.htm
> View this thread: http://forums.techarena.in/server-scripting/1030312.htm
>
> http://forums.techarena.in
>


Try this version of your batch file. BTW, when using "find.exe" then you
should always use the /i switch to make the command robust.
@echo off
set prev=
FOR /F "tokens=2" %%i IN ('tasklist ^| Find /i "notepad.EXE"') DO CALL
:windowlist %%i
goto :EOF

:windowlist
if not "%prev%"=="" echo TASKKILL /PID %prev% /F
set prev=%1


 
Reply With Quote
 
Al Dunbar
Guest
Posts: n/a

 
      09-03-2008

"Pegasus (MVP)" <> wrote in message
news:...
>
> "oztech" <> wrote in message
> news...
>>
>> These word instances are started via automation, and also if you run
>> winword /a then you will get multiple word instances.
>> Tasklist will give you a list of process, in the order of started first
>> to started last. So when I hit the last process in that I want to stop.
>>
>>
>> --
>> oztech
>> ------------------------------------------------------------------------
>> oztech's Profile: http://forums.techarena.in/members/oztech.htm
>> View this thread: http://forums.techarena.in/server-scripting/1030312.htm
>>
>> http://forums.techarena.in
>>

>
> Try this version of your batch file. BTW, when using "find.exe" then you
> should always use the /i switch to make the command robust.
> @echo off
> set prev=
> FOR /F "tokens=2" %%i IN ('tasklist ^| Find /i "notepad.EXE"') DO CALL
> :windowlist %%i
> goto :EOF
>
> :windowlist
> if not "%prev%"=="" echo TASKKILL /PID %prev% /F
> set prev=%1


Good one. I'd make it slightly more robust by adding a "GOTO:EOF" at the
end. That way, if you need to add another internal routine later on, you are
less likely to have the first run on through the next.

BTW, your script kills all but the *last* task, but that is only the last of
the tasks in the task list. Is that the one that is the *LAST* task that the
OP wants to kill?

/Al


 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      09-03-2008

"Al Dunbar" <> wrote in message
news:...
>
> "Pegasus (MVP)" <> wrote in message
> news:...
>>
>> "oztech" <> wrote in message
>> news...
>>>
>>> These word instances are started via automation, and also if you run
>>> winword /a then you will get multiple word instances.
>>> Tasklist will give you a list of process, in the order of started first
>>> to started last. So when I hit the last process in that I want to stop.
>>>
>>>
>>> --
>>> oztech
>>> ------------------------------------------------------------------------
>>> oztech's Profile: http://forums.techarena.in/members/oztech.htm
>>> View this thread:
>>> http://forums.techarena.in/server-scripting/1030312.htm
>>>
>>> http://forums.techarena.in
>>>

>>
>> Try this version of your batch file. BTW, when using "find.exe" then you
>> should always use the /i switch to make the command robust.
>> @echo off
>> set prev=
>> FOR /F "tokens=2" %%i IN ('tasklist ^| Find /i "notepad.EXE"') DO CALL
>> :windowlist %%i
>> goto :EOF
>>
>> :windowlist
>> if not "%prev%"=="" echo TASKKILL /PID %prev% /F
>> set prev=%1


<snip>

> BTW, your script kills all but the *last* task, but that is only the last
> of the tasks in the task list. Is that the one that is the *LAST* task
> that the OP wants to kill?
>
> /Al


This is a question that the OP should probably ask himself. What's so
particular about the last process enumerated by tasklist.exe? That it was
the last one to be launched?


 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a

 
      09-03-2008
"oztech" <> wrote in message
news...
>
> Thanks mate, worked beautifully.
> One quick question, is there a way to find out which Winword process
> amongst these is using CPU more than 20%?
>
> Thanks again.
>
>
> --
> oztech


If you can find a console command that shows CPU percentages then I'm sure
that this can be coded too.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Kill process ivanv Windows Vista Performance 15 11-30-2008 03:39 AM
RE: Kill process Jeremy Viegas Windows Vista Performance 3 09-09-2008 04:59 PM
Re: Kill process Ristar Windows Vista Performance 1 08-29-2008 12:55 AM
Kill Running Process Drummer9909 Scripting 1 10-13-2004 01:41 PM
Script to kill process Brock Heidemann Scripting 2 08-10-2004 11:10 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59