@Echo off
rem Info zur Version:
rem Datum        Wer     Info
rem 10.07.2012   J.Fes   neu erstellt

@setlocal

  set ProgTitle=%~1
  set ProgTitle=Start Notepad
  if "%ProgTitle%"=="" goto :PrintUsage
  rem set ProgTitle="Notepad"

  set Log_DPNE=
  if "%~2" neq "" set Log_DPNE=%~2
  if /i "%~2" equ "default" set Log_DPNE=%~dpn0_Log.txt
  if "%Log_DPNE%" neq "" set Log_DPNE=^>^>"%Log_DPNE%"


  rem feststellen ob ProgTitle bereits gestartet wurde
  tasklist /fi "imagename eq cmd.exe" /v /nh /fo csv | find /i "%ProgTitle%"
  set ProgIsRunning=%errorlevel%
  for /f "delims=, tokens=2" %%a in ('tasklist /fi "imagename eq cmd.exe" /v /nh /fo csv ^| find /i "%ProgTitle%"') do set ProgToKill_PID=%%~a

  rem wenn Prog is running dann diesen beenden
  if "%ProgToKill_PID%" neq "" (
      if exist %Log_DPNE% Echo ---------------------------------------------------------------------%Log_DPNE%
      Echo Stopping...: %ProgTitle%%Log_DPNE%
      Echo PID........: %ProgToKill_PID%
      Echo Date Time..: %Date% %time%%Log_DPNE%
      Echo On.........: \\%ComputerName%%Log_DPNE%
      Echo now kill
      taskkill /PID %ProgToKill_PID% /T /F%Log_DPNE% 2>nul
  )

  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

