tags:

views:

1202

answers:

3

Under a bash environment I usually do: var=$(command -args) and the I use $var with its value setted as the result of the command. The same goes to a more conventional set var=command -args compatible in almost every unix shell.

How could I define a variable in a windows bat file like that? I've tried set var=command -args but I only get the "command -args" string.

+2  A: 

The only way I've seen it done is if you do this:

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

ver is the version command for Windows and on my system it produces:

Microsoft Windows [Version 6.0.6001]

Source

Jesse Dearing
+4  A: 

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

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

But, i will suggest using Cygwin on your Windows system if you are used to unix type scripting.

nik
+1  A: 

Here's how I do it when I need a database query's results in my batch file:

sqlplus -S schema/schema@db @query.sql> __query.tmp
set /p result=<__query.tmp
del __query.tmp

The key is in line 2: "set /p" sets the value of "result" to the value of the first line (only) in "__query.tmp" via the "<" redirection operator.