Windows Batch:如何将一个命令的输出设置为变量,并在另一个命令中使用它? [英] Windows Batch: How to set the output of one command as a variable and use it in another command?

查看:247
本文介绍了Windows Batch:如何将一个命令的输出设置为变量,并在另一个命令中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析一个变量.说出NAMELIST ="AAA BBB CCC"并将每个变量存储为一个变量.然后,必须在另一个命令中使用这些新变量.例如:

I'm trying to parse a variable. Say NAMELIST = "AAA BBB CCC" and store each one as a variable. Then, these new variables must be used in another command. Eg:

perl.exe C:\action.pl <VAR1>
perl.exe C:\action.pl <VAR2>
perl.exe C:\action.pl <VAR3>

我是Windows Batch的新手,所以将不胜感激.

I'm new to Windows Batch so any help would be appreciated.

我知道此线程,但不完全了解解决方案

I am aware of this thread but don't fully understand the solution

Windows批处理文件:如何使用命令结果设置变量?

推荐答案

当您提到将每个变量存储在变量中"时,此处涉及的概念是 array .您可以通过以下方式将NAMELIST变量的单词分为3个数组元素:

When you refer to "store each one in a variable", the involved concept here is array. You may split the words of NAMELIST variable into 3 array elements this way:

setlocal EnableDelayedExpansion
set i=0
for %%a in (%namelist%) do (
   set /A i=i+1
   set VAR!i!=%%a
)

这样,您可以直接使用每个数组元素:

This way, you may use each array element directly:

perl.exe C:\action.pl %VAR1%
perl.exe C:\action.pl %VAR2%
perl.exe C:\action.pl %VAR3%

或者,以更简单的方式使用循环:

Or, in a simpler way using a loop:

for /L %%i in (1,1,3) do perl.exe C:\action.pl !VAR%%i!

编辑:您可以在NAMELIST变量中使用此方法,同时使用无限数量的值,只需使用以前的%i%值代替3(更好的是,将其更改为"n" ).我还建议您以这种方式使用标准数组表示法:VAR[%%i]:

EDIT: You may use this method with an unlimited number of values in the NAMELIST variable, just use the previous value of %i% instead the 3 (better yet, change it by "n"). I also suggest you to use the standard array notation this way: VAR[%%i]:

setlocal EnableDelayedExpansion
set namelist=AAA BBB CCC DDD EEE FFF
set n=0
for %%a in (%namelist%) do (
   set /A n+=1
   set VAR[!n!]=%%a
)
for /L %%i in (1,1,%n%) do perl.exe C:\action.pl !VAR[%%i]!

这篇关于Windows Batch:如何将一个命令的输出设置为变量,并在另一个命令中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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