views:

291

answers:

4

I need to assign the output of a program to a variable using a MS batch file.

So in GNU Bash shell I would use VAR=$(application arg0 arg1). I need a similar behavior in Windows using a batch file.

Something like set VAR=application arg0 arg1.

Thanks!

A: 

assuming that your application's output is a numeric return code, you can do the following

application arg0 arg1
set VAR=%errorlevel%
akf
Unfortunately, the output is a string.
initialZero
ok. i will keep this for posterity, but take a look at @jdigital's link, which talks about piping output to a temp file.
akf
+2  A: 

This article describes two solutions. Unfortunately, the solutions not quite as straightforward as bash.

jdigital
+1  A: 

@OP, you can use for loops to capture the return status of your program, if it outputs something other than numbers

ghostdog74
+1  A: 

One way is:

application arg0 arg1 > temp.txt
set /p VAR=<temp.txt

Another is:

for /f %%i in ('application arg0 arg1') do set VAR=%%i
Carlos Gutiérrez