title: Identify running instances of a batch file
body: <p>These are not working for me. </p>
  
  <p>Any help to definitelly corret the four examples below ?</p>
  
  <p>The EXAMPLE01 just echoes "continue", even if I have three CMD.exe opened. </p>
  
  <p>---------- EXAMPLE 01 ------------</p>
  
  <pre><code>@echo off 
  wmic process where name="cmd.exe" | find "cmd.exe" /c
  SET ERRORLEVEL=value if "%value%" GTR 1 ( 
      ECHO This batch is not first  
      ECHO quitting ...
      )
  if "%value%" LSS 2 ECHO continue
  </code></pre>
  
  <p>I am getting the "unexpected i error" message in the EXAMPLE 02!</p>
  
  <p>----------- EXAMPLE 02 -------</p>
  
  <pre><code>@echo off
  FOR /F "usebackq tokens=2" %i IN (`tasklist ^| findstr /r /b "cmd.exe"`)
     DO taskkill /pid %%i
  </code></pre>
  
  <p>I am getting the "is first" message in the EXAMPLE 03, even with three CMD.exe opened!</p>
  
  <p>----------- EXAMPLE 03 -------</p>
  
  <pre><code> @echo off
      wmic process where name="cmd.exe" | find "cmd.exe" /c
      if "%errorlevel%" LEQ 1 echo CMD is first
      if "%errorlevel%" GTR 1 echo CMD is already running
  </code></pre>
  
  <p>It is also possible that I will not have access to the Wmic command at work, so, another possibility is found in the EXAMPLE 04 ... but to no avail.</p>
  
  <p>----------- EXAMPLE 04 -------</p>
  
  <pre><code>@echo off
  Tasklist /FI "IMAGENAME eq cmd.exe" 2&gt;NUL | find /I /N "cmd.exe"&gt;NUL
  if "%ERRORLEVEL%"==0 do (goto Use) else (goto Cont)
  :Cont
  ECHO Only one instance running
  pause
  
  :Use
  echo Application running already. Close this window
  </code></pre>
  
  <hr>
  
  <p>Kind regards,
  Maleck</p>