我目前正在使用这种方法来自" jeb"处理许多变量。 BAT-file: variable contents as part of another variable
setlocal EnableDelayedExpansion
set arr[1]=17
set arr[2]=35
set arr[3]=77
(
set idx=2
set /a var=arr[!idx!]
echo !var!
)
运作良好。问题是我需要再次设置一些变量的值。 我的所有尝试都失败了。
set set arr[!idx!]=test
call set arr[!idx!]=test
set !arr[!idx!]!=test
答案 0 :(得分:1)
你的问题不明确。我不明白你对&#34的意思;我需要再次设置一些变量的值" 。你应该描述你的期望和你真正得到的东西。出于这个原因,我无法为您的问题提供准确答案,但只是一系列示例:
echo off
setlocal EnableDelayedExpansion
rem The target variable is "b1" or "b2", depending on *current* value of "i":
set i=1
set b1=var1
set b2=var2
call echo %%b%i%%%
rem This works, but only *outside parentheses*
set b%i%=var1changed
call echo %%b%i%%%
rem Inside paren, this works only when "i" var is not changed:
(
set b%i%=var1changed again
call echo %%b%i%%%
set i=2
set b%i%=wrong: this should be b2, but it is b1
call echo %%b%i%%%
)
set b
rem Using delayed expansion for the index works always:
(
set i=1
set b!i!=var1 again, inside paren
call echo %%b!i!%%
set i=2
set b!i!=var2changed
call echo %%b!i!%%
)
set b
输出:
var1
var1changed
var1changed again
wrong: this should be b2, but it is b1
b1=wrong: this should be b2, but it is b1
b2=var2
var1 again, inside paren
var2changed
b1=var1 again, inside paren
b2=var2changed
您也可以使用FOR /F
命令进行此管理。有关详情,请访问this answer。
答案 1 :(得分:0)
我发现(在jeb链接本身)没有任何优雅的解决方案,我仍然需要更改我的所有脚本,所以我仍在等待回复。
set i=1
set b1=var1
call echo %%b%i%%%
set b%I%=var1changed
call echo %%b%i%%%