Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help w FOR Loop in Batch File 1

Status
Not open for further replies.

tobjob

IS-IT--Management
Sep 17, 2009
10
0
0
US
I am trying to take the PID output of the tasklist command and send it to the taskkill command. I have successfully createed a FOR loop to go through the values of the tasklist command and echo out the PID of the process. Mstsc.exe is used as just an example process to kill.

for /F "tokens=2 skip=3" %%A in ('tasklist /FI "IMAGENAME eq mstsc.exe"') do echo %%A

The issue is when I try to add the taskkill command to the do portion of the for loop.

for /F "tokens=2 skip=3" %%A in ('tasklist /FI "IMAGENAME eq mstsc.exe"') do taskill /F /PID %%A.

When I execute the for loop in a batch file it does not recognize the taskkill command. Any help would be appreciated.
 
Your call to taskkill is missing a K that's why its not recognized.

Code:
 do [b]tas[red]k[/red]ill[/b] /F /PID %%A.  

should be:

tas[red]kk[/red]ill

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Note that tasklist and taskkill only work on the Prof version. They don't work on the home version.
 
Thanks for pointing out what should have been obvious...

It works now that I have spelled taskkill correctly.
 
Also note that taskkill accepts the same parameters as tasklist so I think a simple
Code:
'taskkill /FI "IMAGENAME eq mstsc.exe"
would do the trick.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top