@echo off
rem Description: returns how many times an Application is alread excuting / running
rem Datum        Wer     Info
rem 23.03.2023   J.Fes   New


if "%2" neq "" goto :Start

rem #Dbug examples
Call "%~0" "JobClient.exe" JobClient_is_NtimesActive
Echo JobClient_is_NtimesActive=%JobClient_is_NtimesActive%
REM
Call "%~0" "cmd.exe" CMD_is_NtimesActive
Echo CMD_is_NtimesActive=%CMD_is_NtimesActive%
pause
REM
Exit

:Start
if "%~2" equ "" (
 Echo Err: [Call %0 %*]
 Echo Err: missing paramters
 Echo Inf: P1=Prog_NE P2=EnvVarReturnsResuts
 Echo Example: Job starten
 Echo call "ProgIsRunningNtimes.cmd" "JobClient.exe" JobClient_is_NtimesActive
 goto :eof
 )
setlocal
 rem -- Rootpfade setzen
 set PrgToRun_NE=%~1
 if not defined PrgRunsNtimes set PrgRunsNtimes=0
 set TempFileRandom_DPNE=%temp%\%RANDOM%_Info_%RANDOM%.txt
 rem Echo TempFileRandom_DPNE="%TempFileRandom_DPNE%"
 tasklist /FI "IMAGENAME eq %PrgToRun_NE%*" /NH | findstr /i /c:"%PrgToRun_NE%" >>"%TempFileRandom_DPNE%"
 FOR /F "delims=" %%i in (%TempFileRandom_DPNE%) do Call :CountMe "%%i"
 del "%TempFileRandom_DPNE%"
 goto :myEnd

:CountMe
  set /a PrgRunsNtimes=%PrgRunsNtimes% + 1
goto :eof

:myEnd
endlocal& set %2=%PrgRunsNtimes%
goto :eof
