Assign output of a program to a variable using a MS batch file
batch-file, cmd, variable-assignment, windows-console
Solution
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
Note that the first `%` in `%%i` is used to escape the `%` after it and is needed when using the above code in a batch file rather than on the command line. Imagine, your `test.bat` has something like:
for /f %%i in ('c:\cygwin64\bin\date.exe +"%%Y%%m%%d%%H%%M%%S"') do set datetime=%%i
echo %datetime%
Problem
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`. Similar Questions - How to set commands output as a variable in a batch file - How do I get the result of a command in a variable in windows? - Set the value of a variable with the result of a command in a Windows batch file - Set output of a command as a variable (with pipes) - Assign command output to variable in batch file
Related problems
- Set output of a command as a variable (with pipes)
- Assign command output to variable in batch file
- Redirect output of command in for loop of batch script
- How to set commands output as a variable in a batch file
- Set the value of a variable with the result of a command in a Windows batch file
- How do I get the result of a command in a variable in windows?