Share this page 

Capture the output of a program and set environment variableTag(s): Misc Prog HowTo


This will capture the output of program or another batch file and set a given environment variable with the captured content. Only the last line is kept!

[setvar.cmd]

@Echo off
for /f "delims=" %%i in ('%2') do set %1=%%i
Set %1
To use it
> setvar myvar ver

myvar=Microsoft Windows [Version 10.0.14393]
Let say you have another command file

[realhow.cmd]

@echo Hello world from Real's HowTo
Then you can capture its output in an environment variable with
> setvar myvar realhowto

myvar=Hello world from Real's HowTo