使用MS批处理文件将程序的输出分配给变量 [英] Assign output of a program to a variable using a MS batch file

查看:65
本文介绍了使用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:\cygwin64\bin\date.exe +"%%Y%%m%%d%%H%%M%%S"') do set datetime=%%i
echo %datetime%

这篇关于使用MS批处理文件将程序的输出分配给变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆