批量扩展嵌套变量,无延迟扩展

发布于 2025-01-11 16:38:51 字数 449 浏览 0 评论 0原文

是否可以在不延迟扩展的情况下扩展嵌套变量?

示例:

for /L %%i in (0,1,5) do ( echo.%somevar[%%i]% )

这是我想要做的更详细的示例(我无法在此处粘贴我的整个代码)

::%1 list of keys
::%2 output variable
SETLOCAL ENABLEDELAYEDEXPANSION
::parse some files and process information. Then it is stored into somevar
ENDLOCAL & (
    for /L %%i in (0,1,%somevar.count%) do (
        set "%2[%somevar[%%i].key%]=%somevar[%%i].value%"
    )
)

Is it possible to expand a nested variable without delayed expansion?

Example:

for /L %%i in (0,1,5) do ( echo.%somevar[%%i]% )

Here a more detailed example of what I'm trying to do (I cant paste my whole code here)

::%1 list of keys
::%2 output variable
SETLOCAL ENABLEDELAYEDEXPANSION
::parse some files and process information. Then it is stored into somevar
ENDLOCAL & (
    for /L %%i in (0,1,%somevar.count%) do (
        set "%2[%somevar[%%i].key%]=%somevar[%%i].value%"
    )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-01-18 16:38:51

这里有一些想法给您,尽管我建议您使用延迟扩展,如第三个和第四个示例中所示,并且建议您使用第四个:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "somevar[0]=string zero"
Set "somevar[1]=string one"
Set "somevar[2]=string two"
Set "somevar[3]=string three"
Set "somevar[4]=string four"
Set "somevar[5]=string five"

Echo(
For /L %%G In (0,1,5) Do Call Echo(%%somevar[%%G]%%
Echo(
For /L %%G In (0,1,5) Do For /F Delims^=^ EOL^= %%H In ('Echo(%%somevar[%%G]%%') Do Echo(%%H
Echo(
For /L %%G In (0,1,5) Do %SystemRoot%\System32\cmd.exe /V /D /C Echo(!somevar[%%G]!
Echo(
For /L %%G In (0,1,5) Do SetLocal EnableDelayedExpansion & Echo(!somevar[%%G]!& EndLocal
Pause

Here's a few ideas for you, although I would recommend that you use delayed expansion, as in the third and fourth examples, and of those, advise that you use the fourth:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "somevar[0]=string zero"
Set "somevar[1]=string one"
Set "somevar[2]=string two"
Set "somevar[3]=string three"
Set "somevar[4]=string four"
Set "somevar[5]=string five"

Echo(
For /L %%G In (0,1,5) Do Call Echo(%%somevar[%%G]%%
Echo(
For /L %%G In (0,1,5) Do For /F Delims^=^ EOL^= %%H In ('Echo(%%somevar[%%G]%%') Do Echo(%%H
Echo(
For /L %%G In (0,1,5) Do %SystemRoot%\System32\cmd.exe /V /D /C Echo(!somevar[%%G]!
Echo(
For /L %%G In (0,1,5) Do SetLocal EnableDelayedExpansion & Echo(!somevar[%%G]!& EndLocal
Pause

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文