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
.
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
.
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%
As an addition to this previous answer, pipes can be used inside a for statement, escaped by a caret symbol:
for /f \"tokens=*\" %%i in (\'tasklist ^| grep \"explorer\"\') do set VAR=%%i
@OP, you can use for loops to capture the return status of your program, if it outputs something other than numbers
assuming that your application\'s output is a numeric return code, you can do the following
application arg0 arg1
set VAR=%errorlevel%
On Executing: for /f %%i in (\'application arg0 arg1\') do set VAR=%%i
i was getting error: %%i was unexpected at this time.
As a fix, i had to execute above as for /f %i in (\'application arg0 arg1\') do set VAR=%i
In addition to the answer, you can\'t directly use output redirection operators in the set part of for
loop (e.g. if you wanna hide stderror output from a user and provide a nicer error message). Instead, you have to escape them with a caret character (^
):
for /f %%O in (\'some-erroring-command 2^> nul\') do (echo %%O)
Reference: Redirect output of command in for loop of batch script
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM Prefer backtick usage for command output reading:
REM ENABLEDELAYEDEXPANSION is required for actualized
REM outer variables within for\'s scope;
REM within for\'s scope, access to modified
REM outer variable is done via !...! syntax.
SET CHP=C:\\Windows\\System32\\chcp.com
FOR /F \"usebackq tokens=1,2,3\" %%i IN (`%CHP%`) DO (
IF \"%%i\" == \"Aktive\" IF \"%%j\" == \"Codepage:\" (
SET SELCP=%%k
SET SELCP=!SELCP:~0,-1!
)
)
echo actual codepage [%SELCP%]
ENDLOCAL
I wrote the script that pings google.com every 5 seconds and logging results with current time. Here you can find output to variables \"commandLineStr\" (with indices)
@echo off
:LOOPSTART
echo %DATE:~0% %TIME:~0,8% >> Pingtest.log
SETLOCAL ENABLEDELAYEDEXPANSION
SET scriptCount=1
FOR /F \"tokens=* USEBACKQ\" %%F IN (`ping google.com -n 1`) DO (
SET commandLineStr!scriptCount!=%%F
SET /a scriptCount=!scriptCount!+1
)
@ECHO %commandLineStr1% >> PingTest.log
@ECHO %commandLineStr2% >> PingTest.log
ENDLOCAL
timeout 5 > nul
GOTO LOOPSTART