Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Windows scripting noob here. Say I've got the name of a .dll I need to delete or copy over, and I'm getting the classic "The file is in use by another process..." error. I know I can run tasklist with the /m switch and pass it the dll name. Then I can get the PID for the processes and kill them with taskkill.

How can I do this in one step in a batch file?

share|improve this question
add comment

4 Answers

up vote 2 down vote accepted

Reminder: in a batch file you will need to use the double "%%" instead a single

FOR /F "usebackq tokens=2 skip=2" %i IN (`tasklist /m winsta.dll`) DO taskkill /PID %i
share|improve this answer
1  
this is awesome, you are awesome –  Chris McCall Dec 15 '09 at 17:18
    
Well thank you! –  Goyuix Dec 15 '09 at 20:48
add comment

FOR /F "usebackq tokens=2 skip=2" %%i IN (TASKLIST /FI "IMAGENAME eq tomcat6.exe") DO taskkill /F /PID %%i

will automatically kill the tomcat process.

share|improve this answer
add comment

Goyuix,

I can't seem to get the tasklist results to parse. FOR /F "usebackq tokens=2 skip=2" %i IN ('tasklist /m winsta.dll') DO echo %i The tasklist command by itself is fine, but the loop is not recognizing the tasklist resuls. Rather it appears to be parsing the string. I can output the task list results to file and parse with the loop, but I thought I might be missing something simple.

share|improve this answer
    
use a backtick (same key as the tilde on most U.S. layout keyboards) - not the single quote. Try that. –  Goyuix Dec 15 '09 at 21:21
add comment

yep. simple. backquote, not single quote.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.