Windows Batch变量内的变量 [英] Windows Batch Variable within variable
本文介绍了Windows Batch变量内的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Windows批处理中,您可以在变量中设置变量吗?
In windows batch could you set a variable within a variable?
解释:
因此%num%在变量内.
So the %num% is within the variable.
set num=5
set cnum=test
set h1=%c%num%%
是否可以使百分数像括号一样工作? 输出应为h1 = test
Is it possible to make the percents work like parenthesis? The output should be h1=test
任何帮助将不胜感激.
推荐答案
您在问题中遇到的例子一团糟,但我认为我明白您在寻找什么:
Your example in your question is a mess, but I think I understand what you are looking for:
@echo off
setlocal
set C1=apple
set C2=orange
set C3=banana
set num=2
:: Inefficient way without delayed expansion
:: This will be noticeably slow if used in a tight loop with many iterations
call echo %%C%num%%%
:: The remaining methods require delayed expansion
setlocal enableDelayedExpansion
:: Efficient way 1
echo(
echo !C%num%!
:: Efficient way 2 - useful if inside parenthesized block
:: where %num% will not give current value
echo(
for %%N in (!num!) do echo !C%%N!
:: Showing all values via a loop
echo(
for /l %%N in (1 1 3) do echo !C%%N!
这篇关于Windows Batch变量内的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文