使用 ENABLEDELAYEDEXPANSION

发布于 2024-11-18 22:51:56 字数 70 浏览 1 评论 0 原文

在批处理脚本中使用 ENABLEDELAYEDEXPANSION 时,调用 ENDLOCAL 后在其中创建的变量是否仍然存在?

When using ENABLEDELAYEDEXPANSION in a batch script, do the variables that are created within it still exist after calling ENDLOCAL?

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

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

发布评论

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

评论(1

束缚m 2024-11-25 22:51:56

我理解,您的问题基本上是关于 SETLOCAL 命令及其效果,无论使用什么 ENABLEDELAYEDEXPANSION 选项(或任何其他选项)。

我的简短回答是:,假设变量在进入 SETLOCAL 范围之前不存在。

我的较长答案如下:

SETLOCAL 范围内对变量所做的所有更改在退出范围时(即到达 ENDLOCAL 时)都会被丢弃。这包括:

  • 定义以前未定义的变量:

    <前><代码>@ECHO 关闭

    输出:

    <前><代码>1.未定义
    2.定义
    3.未定义

  • 取消定义先前定义的变量:

    <前><代码>@ECHO 关闭

    设置ttt=1
    <nul SET /P q=1。
    IF DEFINED ttt(ECHO 已定义) ELSE(ECHO 未定义)

    设定本地

    设置TT=
    <nul SET /P q=2。
    IF DEFINED ttt(ECHO 已定义) ELSE(ECHO 未定义)

    本地化

    <nul SET /P q=3。
    IF DEFINED ttt(ECHO 已定义) ELSE(ECHO 未定义)

    输出为:

    <前><代码>1.定义
    2.未定义
    3.定义

  • 更改变量的值:

    <前><代码>@ECHO 关闭

    设置ttt=1
    回声1.ttt=%ttt%

    设定本地

    设置ttt=2
    回声2.ttt=%ttt%

    本地化

    回声3.ttt=%ttt%

    这会产生以下输出:

    <前><代码>1.ttt=1
    2.ttt=2
    3.ttt=1

正如我在开始时所说,上述内容适用于 SETLOCAL 无论您是否将其与其他选项一起使用。

总之,我想说的是,可以保存SETLOCAL范围内计算的结果,以便在ENDLOCAL之后使用。这里有一个小技巧可以实现这一点:

…
ENDLOCAL & SET var=%var%
…

解析这一行时,SETLOCAL命令仍然有效,因此%var%被评估为您最近存储到 var 中的值。当该行执行时,var变量在ENDLOCAL之后立即丢失其值,但SET命令已经包含其值,只是被替换,所以var 收到了它,每个人都满意。

根据 @Jeremy Murray 的评论,如果您包含 ENDLOCAL 以及在所包含的单个块中读取变量的命令,您还可以在 ENDLOCAL 之后访问更改后的值括号中:

…
(
ENDLOCAL
SET var=%var%
anything else
)
…

效果是相同的,因为括号内的命令都作为一个单元进行解析和执行:首先它们全部被解析,然后它们全部被执行。

I understand, your question is basically about the SETLOCAL command and its effects, regardless of the ENABLEDELAYEDEXPANSION option (or any other one) used.

My short answer is: No, assuming the variables didn't exist prior to entering SETLOCAL's scope.

My longer answer is as follows:

All the changes made to a variable within the scope of SETLOCAL are discarded upon exiting the scope (i.e. upon reaching ENDLOCAL). This includes:

  • defining a previously undefined variable:

    @ECHO OFF
    
    <nul SET /P q=1.
    IF DEFINED ttt (ECHO defined) ELSE (ECHO undefined)
    
    SETLOCAL
    
    SET ttt=
    <nul SET /P q=2.
    IF DEFINED ttt (ECHO defined) ELSE (ECHO undefined)
    
    ENDLOCAL
    
    <nul SET /P q=3.
    IF DEFINED ttt (ECHO defined) ELSE (ECHO undefined)
    

    This outputs:

    1.undefined
    2.defined
    3.undefined
    
  • undefining a previously defined variable:

    @ECHO OFF
    
    SET ttt=1
    <nul SET /P q=1.
    IF DEFINED ttt (ECHO defined) ELSE (ECHO undefined)
    
    SETLOCAL
    
    SET ttt=
    <nul SET /P q=2.
    IF DEFINED ttt (ECHO defined) ELSE (ECHO undefined)
    
    ENDLOCAL
    
    <nul SET /P q=3.
    IF DEFINED ttt (ECHO defined) ELSE (ECHO undefined)
    

    The output is:

    1.defined
    2.undefined
    3.defined
    
  • changing a variable's value:

    @ECHO OFF
    
    SET ttt=1
    ECHO 1.ttt=%ttt%
    
    SETLOCAL
    
    SET ttt=2
    ECHO 2.ttt=%ttt%
    
    ENDLOCAL
    
    ECHO 3.ttt=%ttt%
    

    And this produces the following output:

    1.ttt=1
    2.ttt=2
    3.ttt=1
    

As I said in the beginning, the above applies to SETLOCAL regardless of whether you use it with additional options or not.

In conclusion I'd like to say that it is possible to save the result calculated within SETLOCAL's scope, for use after ENDLOCAL. Here's a little trick that makes it possible:

…
ENDLOCAL & SET var=%var%
…

At the time of parsing this line, the SETLOCAL command is still in effect, so %var% gets evaluated to the value you've stored into var most lately. When the line is executed, the var variable loses its value immediately after ENDLOCAL, but the SET command already contains its value, just substituted, so var receives it back, to everybody's satisfaction.

As per @Jeremy Murray's comment, you could also get access to the changed value after ENDLOCAL if you included ENDLOCAL and the command(s) reading the variable in a single block enclosed in parentheses:

…
(
ENDLOCAL
SET var=%var%
anything else
)
…

The effect would be the same because bracketed commands are both parsed and executed as a single unit: first they are all parsed, then they are all executed.

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