Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to set an environment variable to the output of a command in cmd.exe

I need to do the equivalent of

set ENVAR=`some-command`

In a windows/cmd.exe script. Cygwin is not an option.

For bonus marks: Is there some cmd.exe equivalent of backticks in general?

like image 534
Peter Graham Avatar asked Jul 07 '10 23:07

Peter Graham


1 Answers

A quick and dirty way would be redirecting it to a file and then reading this, e.g.

some-command>out.txt
set /p ENVAR=<out.txt

I think for can also help you, but I don't remember the exact syntax. Try something like

for /f "usebackq" %x in (`some-command`) do set ENVAR=%x

I probably forgot some token or delim in the options...

like image 50
mhd Avatar answered Oct 04 '22 00:10

mhd