3

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?

Chris McCall
  • 348
  • 1
  • 3
  • 8

4 Answers4

4

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
Goyuix
  • 3,214
  • 5
  • 29
  • 37
  • be aware that "skip=2" means to skip the first two lines of output, in case you want to tweak this (ref: http://stackoverflow.com/questions/9712399/pass-pids-from-tasklist-and-kill-processes-with-tasklist) – rogerdpack Jun 24 '15 at 09:25
1

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.

0

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.

  • 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
0

yep. simple. backquote, not single quote.