简体   繁体   English

如何在Windows Batch中传递数组参数?

[英]How to passing in array parameter in windows batch?

I'm trying to run this command in batch file: 我正在尝试在批处理文件中运行以下命令:

wmic nicconfig where macaddress=somemacaddr call SetDNSServerSearchOrder (an array paramter)

For example: 例如:

set dnslist[1]="172.12.3.1"
set dnslist[2]="222.123.2.1"
...
set dnslist[x]="135.132.1.2"

We don't know the dnslist size before running the batch. 在运行批处理之前,我们不知道dnslist大小。 How could we passing the dnslist to SetDNSServerSearchOrder directly? 我们如何将dnslist直接传递给SetDNSServerSearchOrder

There are no arrays in batch files, per se. 批处理文件本身没有数组。 What you have there is just a collection of environment variables with the same prefix. 您所拥有的只是一组具有相同前缀的环境变量。 There's nothing special about that. 没什么特别的。

How to pass them to a command depends on the command (eg are they space-separated, ie individual arguments, or comma-separated, or something else entirely?). 如何将它们传递给命令取决于命令(例如,它们是用空格分隔的,即单个参数,还是用逗号分隔,或完全其他?)。 You need to create a string from those variables that matches the format the program expects, eg when they should be space-separated, create a string the following way: 您需要从那些与程序期望的格式相匹配的变量中创建一个字符串,例如,当它们应该用空格分隔时,请按以下方式创建一个字符串:

setlocal enabledelayedexpansion
for /f "delims==" %%A in ('set dnslist[') do set List=!List! %%B

wmic nicconfig where macaddress=somemacaddr call SetDNSServerSearchOrder %List%

Likewise for different delimiters. 对于不同的定界符也是如此。 Delayed expansion should ideally be enabled at the very start of your script; 理想情况下,应该在脚本开始时就启用延迟扩展。 no use creating a new local environment in the middle of it, usually. 通常,在其中间创建新的本地环境没有用。

If you want to call the command once for every entry in your "list", you don't need to create the delimiter-separated list in the first place, but rather just call the command directly with the entry. 如果要为“列表”中的每个条目调用一次命令,则无需首先创建以定界符分隔的列表,而只需直接与条目一起调用命令即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM