本文介绍了使用 MS 批处理文件将程序的输出分配给变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 MS 批处理文件将程序的输出分配给变量.
I need to assign the output of a program to a variable using a MS batch file.
所以在 GNU Bash shell 中我会使用 VAR=$(application arg0 arg1)
.我需要在 Windows 中使用批处理文件进行类似的行为.
So in GNU Bash shell I would use VAR=$(application arg0 arg1)
. I need a similar behavior in Windows using a batch file.
类似于set VAR=application arg0 arg1
.
推荐答案
一种方法是:
application arg0 arg1 > temp.txt
set /p VAR=<temp.txt
另一个是:
for /f %%i in ('application arg0 arg1') do set VAR=%%i
注意%%i
中的第一个%
是用来转义后面的%
,在使用上面的代码时需要用到批处理文件而不是命令行.想象一下,你的 test.bat
有类似的东西:
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:cygwin64indate.exe +"%%Y%%m%%d%%H%%M%%S"') do set datetime=%%i
echo %datetime%
这篇关于使用 MS 批处理文件将程序的输出分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!