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

John Savill

January 8, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

A. Some programs return values to the command line and it may be youwant 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 itin a FOR statement.

For /f "Tokens=*" %i in ('command') do setvariable="%i"

For example:

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

C:>set NTVersion="Windows NT Version 4.0  "

C:>echo %NTVersion%
"Windows NT Version 4.0  "

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

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

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like