Windows Batch Variable within variable

后端 未结 3 1599
夕颜
夕颜 2020-12-11 08:01

In windows batch could you set a variable within a variable?

Explained:

So the %num% is within the variable.

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


        
3条回答
  •  無奈伤痛
    2020-12-11 08:30

    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!
    

提交回复
热议问题