How can I force the output of a program into an environment variable?

A. Some programs return values to the command line and it may be you want these in a variable so they can be viewed/queried by other processes.

The easiest way to put the result into an environment variable is to trap it in a FOR statement.

<b>For /f "Tokens=*" %i in ('command') do set

For example:

C:\&gt;For /f "Tokens=*" %i in ('ver') do set NTVersion="%i"<br><br>
C:\&gt;set NTVersion="Windows NT Version 4.0  "<br><br>
C:\&gt;echo %NTVersion%<br>
"Windows NT Version 4.0  "

If you place the command in a batch file you require two % in front of i, e.g.

<b>For /f "Tokens=*" %%i in ('ver') do set NTVersion="%%i"</b>

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.