title: How to process 'net use' command error output in batch file with a FOR loop?
body: <p>I'm using <code>net use</code> command in batch file to connect a remote location.<br>
  I want to store the output of it into a variable and process it.<br>
  When the command completes successfully, my code works fine.</p>
  
  <p>However, if there is some error, like wrong password, then I'm not able to store the error output in a variable. It directly prints to the console window in which script is running. Please help with the following:</p>
  
  <ol>
  <li>Why does this happen?<br>
  Why I'm unable to store output in variable when error occurs in the command output?</li>
  <li>Is there a suitable way to log ERROR output of the 'net use' command?</li>
  </ol>
  
  <p>Following is my code:</p>
  
  <pre><code>:connNEWlocation
  echo Connecting required remote location...
  for /f "tokens=1,2,3,4,5,6,7,8 delims=*" %%a IN ('net use  \\!hostaddress!\!user! /user:!user! !user_pass!') DO (
      if "%%a"=="The command completed successfully." (
          echo Connected successfully^!
      ) else (
          echo ERROR:"%%a"
          echo do something based on error obtained here                      
      )
  )
  </code></pre>