Echo batch file arrays using a variable for the index?

后端 未结 3 1990
陌清茗
陌清茗 2020-12-20 23:42

If I have a batch file and I am setting arrays with an index that is a variable

@echo off
SET x=1
SET myVar[%x%]=happy

How do I echo that t

3条回答
  •  北海茫月
    2020-12-21 00:26

    SET x=1
    SET myVar[%x%]=happy
    
    call echo %%myvar[%x%]%%
    set myvar[%x%]
    for /f "tokens=2* delims==" %%v in ('set myvar[%x%]')  do @echo %%v
    setlocal enableDelayedExpansion
    echo !myvar[%x%]!
    endlocal
    

    I would recommend you to use

    setlocal enableDelayedExpansion
    echo !myvar[%x%]!
    endlocal
    

    as it is a best performing way

提交回复
热议问题