title: How to redirect error stream to variable
body: <p>I have the following piece of code. I want to take the error stream, put it in variable after executing the command in the <code>for</code> loop, and in the meantime I discard the <code>stdout</code> by sending it to <code>NUL</code>.  </p>
  
  <pre><code> for /f "delims=" %%a IN ('%test_command% 2^&gt;^&amp;1 1^&gt;NUL') do (
             set err_msg="%%~a"
      )
  </code></pre>
  
  <p>I wish to add one feature to it. In addition to sending <code>stdout</code> to <code>NUL</code> I want to store it in a file, how do I to add that functionality ?</p>
  
  <p>Using the below code I can store the output to a file:</p>
  
  <pre><code>for /f "delims=" %%a IN ('%test_command% 2^&gt;^&amp;1 1^&gt;temp.txt') do (
             set err_msg="%%~a"    
  )
  </code></pre>
  
  <p>But I want to store the output to a variable, how can I do that?</p>