title: BATCH — Store command output to variable
body: <p>I would like to make a bat script that performs an action only if fewer than 2 occurrences of cmd.exe are running. I managed to find a solution that stores the occurrence count in a temporary file, but I find it very inelegant. So my question is how to do the same as below without a temporary file, but rather by storing the occurence count given by <code>@TASKLIST | FIND /I /C "%_process%"</code> in a variable. I saw that there may be a solution with a for loop, but I couldn’t get it to work and anyway I would really prefer a solution based on <code>SET</code> (if this is possible).</p>
  
  <pre><code>@SET _process=cmd.exe
  @SET _temp_file=tempfiletodelete.txt
  @TASKLIST | FIND /I /C "%_process%" &gt; %_temp_file%
  @SET /p _count=&lt;%_temp_file%
  @DEL /f %_temp_file%
  @IF %_count% LSS 2 (
      @ECHO action 1
  ) ELSE (
      @ECHO action 2
  )
  </code></pre>
  
  <p><strong>Edit:</strong> This question is similar to <a href="https://stackoverflow.com/questions/9524239/save-output-from-find-command-to-variable">Save output from FIND command to variable</a>, but I wasn’t able to apply the solution to my problem and I wanted to know if a solution without a for loop is possible. </p>