繁体   English   中英

变量中的Windows批处理变量

[英]Windows Batch Variable within variable

在Windows批处理中,您可以在变量中设置变量吗?

解释:

所以%num%在变量范围内。

set num=5
set cnum=test
set h1=%c%num%%

是否有可能使百分数像括号一样工作? 输出应为h1 = test

任何帮助,将不胜感激。

您可能正在寻找“呼叫设置”命令。 这将cnum设置为字符串c1,c2,c3等。每次%num%更改时它都会更改。 然后,您可以使用“调用集”将任何变量(例如h1)分配给变量cnum所代表的值。

@echo off
setlocal enabledelayedexpansion
    set c5=test
    set num=5

    :: set cnum to string "c5"
        set cnum=c%num%
    :: set h1 to to the existing variable c5 
        call set "h1=%%%cnum%%%"
        echo %h1%

你问题中的例子很乱,但我想我明白你在寻找什么:

@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!

你是在经历这样的事吗?

cls
@echo off
setlocal EnableDelayedExpansion
Set num=5
Set "c!num!=test"
Echo !c5!

有关延迟扩展的详细信息,请访问http://ss64.com/nt/delayedexpansion.html

cls
@echo off
setlocal EnableDelayedExpansion
set num=4
set c1=this is a demo
set c2=second example
set c3=third
set c4=this is the one I want
set c5=last
Echo !c%num%!

暂无
暂无

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

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