首页 > 解决方案 > 如何批量嵌套变量

问题描述

我一直在制作菜单系统,但遇到了一个问题:我必须使用一个变量来跟踪所选项目,以指定我想在列表中使用的项目。我知道这个问题之前已经被问过,但我还没有找到合适的答案

set new_option[%selected%]=%selection_symbol%%org_option[%selected%]%

嵌套变量是“%org_option[%selected%]%”

问题是批处理将变量拆分为“%org_option[%”、“selected”和“%]%”,但我希望将其拆分,以便使用“selected”变量来定义列表,意味着“selected”嵌套在“org_option[]”中

标签: batch-file

解决方案


您实际上是在尝试引用数组变量。你有两个选择来解决这个问题。一种是使用 CALL 命令来获得额外的变量扩展层,这需要您将外部变量上的百分比符号加倍,另一种需要您在代码中启用延迟扩展并使用感叹号引用外部变量。

 CALL set new_option[%selected%]=%selection_symbol%%%org_option[%selected%]%%

或者

 Setlocal enabledelayedexpansion
 set new_option[%selected%]=%selection_symbol%!org_option[%selected%]!

推荐阅读