2

I want to write a batch file that updates a DLL that is in use by a running process, a regular application.

To do this, the plan is to stop the process, copy the DLL to the required location, then restart the process.

I know I can try to kill a process with taskkill. How can I make sure the process has fallen over and died, after I shoot it?

1 Answer 1

4

Here's what I used. It's a subroutine in a batch file.

set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe

-------------------------------------------------------
:STOPPROC
    set wasStopped=0
    set procFound=0
    set notFound_result=ERROR:
    set procName=%1
    for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
      if NOT %%A==%notFound_result% (set procFound=1)
    )
    if %procFound%==0 (
      echo The process was not running.
      goto :EOF
    )
    set wasStopped=1
    set ignore_result=INFO:
:CHECKDEAD
    "%windir%\system32\timeout.exe" 3 /NOBREAK
    for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
      if not %%A==%ignore_result% (goto :CHECKDEAD)
    )
    goto :EOF
-------------------------------------------------------

To use it from within a batch file, do like this:

  call :STOPPROC notepad.exe

Full example:

set tasklist=%windir%\System32\tasklist.exe
set taskkill=%windir%\System32\taskkill.exe

-------------------------------------------------------
:STOPPROC
    set wasStopped=0
    set procFound=0
    set notFound_result=ERROR:
    set procName=%1
    for /f "usebackq" %%A in (`%taskkill% /IM %procName%`) do (
      if NOT %%A==%notFound_result% (set procFound=1)
    )
    if %procFound%==0 (
      echo The process was not running.
      goto :EOF
    )
    set wasStopped=1
    set ignore_result=INFO:
:CHECKDEAD
    "%windir%\system32\timeout.exe" 3 /NOBREAK
    for /f "usebackq" %%A in (`%tasklist% /nh /fi "imagename eq %procName%"`) do (
      if not %%A==%ignore_result% (goto :CHECKDEAD)
    )
    goto :EOF
-------------------------------------------------------

:MAIN 

call :STOPPROC notepad.exe
call :STOPPROC Skype.exe

You'll notice lines that have all dashes - that's not a legal syntax for a batch file of course. But, those lines are never reached, because of the use of GOTO statements, so the syntax is never evaluated. Therefore those lines aren't a problem.

3
  • Sorry - could you let me know how to use this in a batch file? Once I've: call :STOPPROC notepad.exe-d, where do I put the next line I want to execute? It seems like it'll just go to EOF and finish once it's detected it's closed? Jul 19, 2013 at 19:23
  • "goto :EOF" is the syntax to indicate the end of a call. It does not actually "goto end of file". After you do call :STOPPROC notepad.exe the line following call :STOPPROC... will be executed.
    – Cheeso
    Jul 19, 2013 at 21:07
  • Right - looks like I have no idea how to use subroutines in batch files! Need to go to Google! Jul 23, 2013 at 9:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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