Assign output of a program to a variable using a MS batch file

前端 未结 10 991
梦谈多话
梦谈多话 2020-11-22 10:06

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 si

10条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 10:43

    One way is:

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

    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%
    

提交回复
热议问题