在 Windows 批处理文件中打印多个变量表达式的值

发布于 2024-11-18 06:32:39 字数 454 浏览 3 评论 0原文

所以我试图以 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿童谣 2024-11-25 06:32:39

您可能必须使用环境变量的延迟扩展。命令SETLOCAL EnableDelayedExpansion初始化该模式,之后您可以通过将变量括在!而不是%中来使用延迟扩展。

下面是如何使用它的说明:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

ECHO Initialising...
FOR /L %%i IN (1,1,5) DO SET Dog.%%i=!RANDOM!

ECHO Displaying...
FOR /L %%i IN (1,1,5) DO ECHO %%i: !Dog.%%i!

SET index=3
ECHO Dog.%index% is !Dog.%index%!

输出:

Initialising...
Displaying...
1: 1443
2: 6940
3: 24198
4: 8054
5: 14092
Dog.3 is 24198

您可以看到脚本的最后部分同时使用了立即扩展和延迟扩展。立即扩展将 %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:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

ECHO Initialising...
FOR /L %%i IN (1,1,5) DO SET Dog.%%i=!RANDOM!

ECHO Displaying...
FOR /L %%i IN (1,1,5) DO ECHO %%i: !Dog.%%i!

SET index=3
ECHO Dog.%index% is !Dog.%index%!

Output:

Initialising...
Displaying...
1: 1443
2: 6940
3: 24198
4: 8054
5: 14092
Dog.3 is 24198

You can see that the last part of the script employs both the immediate and the delayed expansion. The immediate expansion replaces %index% with 3 and results in the variable name of Dog.3. Then the delayed expansion replaces the !Dog.3! expression with the value of the Dog.3 variable.

勿忘初心 2024-11-25 06:32:39

您正在寻找类似的内容(在批处理/cmd 文件中 - 请注意,这至少需要 XP、IIRC,并且必须启用命令扩展):(

for /l %%i in (1, 1, 10) do set Dog%%i=%%i
for /l %%j in (10, -1, 1) do echo Dog%%j is %%%Dog%%j

请注意,/ 后面的字符是小写 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):

for /l %%i in (1, 1, 10) do set Dog%%i=%%i
for /l %%j in (10, -1, 1) do echo Dog%%j is %%%Dog%%j

(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. :)

落花随流水 2024-11-25 06:32:39
set Dog=(Lassie WonderPup Fido Beethoven)
for %%i in %Dog% do echo %%i
pause
set Dog=(Lassie WonderPup Fido Beethoven)
for %%i in %Dog% do echo %%i
pause
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文