gpt4 book ai didi

batch-file - 在循环中分配超过 endlocal 的变量

转载 作者:行者123 更新时间:2023-12-02 15:29:17 25 4
gpt4 key购买 nike

如何在循环中保留 endlocal 之后的多个变量(具有特定前缀)?

变量在循环声明中可见,但在循环体中不可见。

这是一个使用 echo 而不是变量赋值的示例,以说明变量在 for 主体中不可见。通常,变量赋值会代替 echo:

@echo off

setlocal ENABLEDELAYEDEXPANSION
set TB_1=test1
set TB_2=test2

set TB_ALL_VARS=
for /F "tokens=1 delims==" %%x in ('set TB_') do (
set TB_ALL_VARS=%%x !TB_ALL_VARS!
)

for %%x in (%TB_ALL_VARS%) do ( echo %%x = !%%x! )
echo END LOCAL
endlocal & ( for %%x in (%TB_ALL_VARS%) do ( echo %%x = !%%x! ) )

输出:

TB_2 = test2
TB_1 = test1
END LOCAL
TB_2 = !TB_2!
TB_1 = !TB_1!

如您所见,变量在endlocal之前打印正常,但在endlocal之后打印不出来。

有没有办法像这样保存过去的endlocal变量?

最佳答案

鉴于您的示例代码,我认为您要问的是“如何在 endlocal 之后设置动态变量数?”您的要求不是很直观,但 是可能的。将 setendlocal 复合时,不能使用延迟扩展。通常可以采用一种变通方法,使用 for 循环到 endlocal & set "var=%%A",这仅在变量和值的数量是静态的情况下才有效。不幸的是,endlocal & forfor...in...do (endlocal & set) 的工作方式不同,您肯定会在您的自己测试。

我的解决方案是在 endlocal 之后使用宏进行设置——基本上是将 commands 而不是简单的字符串值放入变量中,然后将该变量作为一组 set 命令。

@echo off
setlocal

:: // call ":set" subroutine to do the setting
call :set

:: // display results
set subv

:: // end main runtime
goto :EOF


:: // :set subroutine
:set
setlocal enabledelayedexpansion

:: // any number of variables prefixed by "subv"
set "subv1=1"
set "subv2=2"
set "subv3=3"

:: // init %compound%
set compound=

:: // combine all %subvX% variables into a macro of set var1=val1 & set var2=val2, etc
for /f "delims=" %%I in ('set subv') do set "compound=!compound! & set "%%~I""

:: // evaluate set commands as a macro
endlocal & %compound:~3%
goto :EOF

另一个解决方案是回到我提到的第一个解决方法,格式为 endlocal & set "var=%%A"。通常这只在你知道你只会循环一次时使用,比如

for %%I in ("!var!") do endlocal & set "return=%%~I"

...因为您不想结束本地化太多次。但是您可以通过使用 if not defined 像这样使用单个 endlocal 执行多值循环:

@echo off
setlocal

call :set
set subv

goto :EOF

:set
setlocal enabledelayedexpansion
set "subv1=1"
set "subv2=2"
set "subv3=3"
set end=
for /f "delims=" %%I in ('set subv') do (
if not defined end endlocal & set end=1
set "%%~I"
)
goto :EOF

... 并且因为 endlocalset "%%~I" 包含在带括号的代码块中,变量被重新设置并且您的目标实现了.

关于batch-file - 在循环中分配超过 endlocal 的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28682268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com