在 Windows 批处理文件中打印多个变量表达式的值
所以我试图以 (variable.variable) 的形式打印出变量的值。我不知道该怎么做。我得到的最接近的东西是:
echo %"%Dog.Index%"%
刚刚打印出来:
Dog.Index
然后我尝试添加另一组 % 标签并打印出来:
%""%
任何帮助都会很棒!谢谢。
(已编辑)
我试图基本上制作一个类似数组的变量。因此,我希望打印出 Dog.1、Dog.2、Dog.3 的值并按如下方式分配它们:
set Dog.%index%=(Value)
%index% 只是我递增的计数器。 尝试“%Dog.index%”不起作用,%Dog%.%index% 也不起作用 再次感谢。
So I'm trying to print out the value of a variable in the form of (variable.variable). I can't figure out how to do it. The closest thing I got was:
echo %"%Dog.Index%"%
Which just printed out:
Dog.Index
And then I tried to add another set of % tags and that printed out:
%""%
Any help would be great! Thanks.
(EDITED)
I'm trying to basically make an array-like variable. So I want the value of Dog.1, Dog.2, Dog.3 printed out and assign them as such:
set Dog.%index%=(Value)
And %index% is just a counter that I increment.
Trying "%Dog.index%" does not work, neither does %Dog%.%index%
Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能必须使用环境变量的延迟扩展。命令
SETLOCAL EnableDelayedExpansion
初始化该模式,之后您可以通过将变量括在!
而不是%
中来使用延迟扩展。下面是如何使用它的说明:
输出:
您可以看到脚本的最后部分同时使用了立即扩展和延迟扩展。立即扩展将
%index%
替换为3
,并生成变量名称Dog.3
。然后,延迟扩展将!Dog.3!
表达式替换为Dog.3
变量的值。You'll probably have to use delayed expansion of environment variables. The command
SETLOCAL EnableDelayedExpansion
initialises the mode, after which you can use the delayed expansion by enclosing variables in!
instead of%
.Here's an illustration of how it can be used:
Output:
You can see that the last part of the script employs both the immediate and the delayed expansion. The immediate expansion replaces
%index%
with3
and results in the variable name ofDog.3
. Then the delayed expansion replaces the!Dog.3!
expression with the value of theDog.3
variable.您正在寻找类似的内容(在批处理/cmd 文件中 - 请注意,这至少需要 XP、IIRC,并且必须启用命令扩展):(
请注意,
/
后面的字符是小写 L。)有关
for
(双关语)的详细信息,请参阅 MSDN TechNet。顺便说一句,在 Win7 上测试过。您的里程可能会有所不同。 :)
You're looking for something like this (in a batch/cmd file - note this requires at least XP, IIRC, and command extensions have to be enabled):
(Note that the character following
/
is a lower-case L.)For more info on
for
(pun intended), see MSDN TechNet.Tested on Win7, BTW. Your mileage may vary. :)