Set the value of a variable with the result of a command in a Windows batch file

batch-file, windows

Solution

To do what Jesse describes, from a Windows batch file you will need to write:

for /f "delims=" %%a in ('ver') do @set foobar=%%a 

But, I instead suggest using Cygwin on your Windows system if you are used to Unix-type scripting.

Problem

When working in a Bash environment, to set the value of a variable as the result of a command, I usually do: ``` var=$(command -args) ``` where `var` is the variable set by the command `command -args`. I can then access that variable as `$var`. A more conventional way to do this which is compatible with almost every Unix shell is: ``` set var=`command -args` ``` That said, how can I set the value of a variable with the result of a command in a Windows batch file? I've tried: ``` set var=command -args ``` But I find that `var` is set to `command -args` rather than the output of the command.

Original source

Related problems