@Goto :Start
rem Info zur Version:
rem Datum        Wer     Info
rem 24.03.2010   J.Fes   ueberarbeitet
rem 19.07.2013   J.Fes   versucht jetzt ein normales beenden des Programmes wenn das nicht kappt dann erfolgt ein force beenden
rem #Usage..: call ProgKill.cmd "#Exe_N#" "#Log_DPNE#" "#WaitTime#"
rem #Example: call ProgKill.cmd "notepad" "" "10"
:Start
@setlocal
  @echo off
  set ExeToRun_N=%~1

  if "%ExeToRun_N%"=="" goto :PrintUsage
  rem set ExeToRun_N="Notepad"

  set Log_DPNE=nul
  if not "%~2"=="" set Log_DPNE=%~2
  if not "%~2"=="" set Log_DPNE="%Log_DPNE%"

  rem #Doc: default Waittime is = 10
  set WaitSec=10
  if "%~3" neq "" set WaitSec=%~3

  rem feststellen ob ExeToRun_N bereits gestartet wurde
  tasklist /FI "IMAGENAME eq %ExeToRun_N%*" 2>nul|findstr /i /c:"%ExeToRun_N%" >nul 2>nul
  set ProgIsRunning=%errorlevel%

  rem wenn Prog is running dann diesen beenden
  if "%ProgIsRunning%" neq "0" goto :JumbEnd1
      if exist %Log_DPNE% Echo --------------------------------------------------------------------->>%Log_DPNE%
      Echo Stopping...: %ExeToRun_N% >>%Log_DPNE%
      Echo Date Time..: %Date% %Time% >>%Log_DPNE%
      Echo On.........: \\%ComputerName% >>%Log_DPNE%
      rem #Step: try first standard methode Stop program
      taskkill /T /FI "IMAGENAME eq %ExeToRun_N%*" >>%Log_DPNE% 2>nul

      rem #Step: look as soon as posible if program stoped
      tasklist /FI "IMAGENAME eq %ExeToRun_N%*" 2>nul|findstr /i /c:"%ExeToRun_N%" >nul 2>nul
      set ProgIsRunning=%errorlevel%

      if "%ProgIsRunning%" neq "0" goto :JumbEnd2
         rem #Step: Wait some seconds for end of program
         Echo Wait Now...: %WaitSec% [sec] for End of Appl: [%ExeToRun_N%]
         Echo Wait Now...: %WaitSec% >>%Log_DPNE%
         ping localhost -n %WaitSec% >nul
         rem #Step: look now if program stoped
         tasklist /FI "IMAGENAME eq %ExeToRun_N%*" 2>nul|findstr /i /c:"%ExeToRun_N%" >nul 2>nul
         set ProgIsRunning=%errorlevel%
         if "%ProgIsRunning%" neq "0" goto :JumbEnd3
             Echo Kill.......: %ExeToRun_N%
             Echo Kill.......: %ExeToRun_N% >>%Log_DPNE%
             taskkill /T /F /FI "IMAGENAME eq %ExeToRun_N%*" >>%Log_DPNE% 2>nul
         :JumbEnd3
      :JumbEnd2
  :JumbEnd1
  
  rem wmic process where name="NXService.exe" delete
  rem wmic process where caption="NXService" delete

  Goto :MyEnd
  
  :PrintUsage
   Echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   Echo Help:%0
   Echo.
   Echo P1 = Name of Program to Stop
   Echo P1 = DPNE Name of Logfile (Info if file exist log info be appendet to logfile)
   Echo.
   Echo Example: Kill's Execution of Program with name Notepad
   Echo Call ProgKill.cmd "Notepad"
   Echo.
   Echo Example: Kill's Execution of Program with name Notepad  and info will be add to given Logfile
   Echo Call ProgKill.cmd "Notepad" "C:\Temp\Exe_Kill_LOG.txt"
   Echo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   Goto :MyEnd
  
  :MyEnd
@endlocal

