How to keep the value of a variable outside a Windows batch script which uses “delayed expansion local” mode?

后端 未结 3 1614
悲&欢浪女
悲&欢浪女 2020-12-10 04:39

Context: I need to call a Windows batch script which would update my PATH by adding another path \'xxx\' at the end of it, but:

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 05:20

    Yay! Finally got this working with the following test code:

    @echo off
    Setlocal enabledelayedexpansion
    
    Set p="hello world"
    
    ( endlocal & rem return
    
       Set "a1=%p%"
    
    )
    
    Set a1
    

    This outputs:

    a1="hello world"
    

    The reason I used delayed expansion in the test without using any !'s is because it still effects how set works and the batchs I'm testing this for all have delayed expansion.

    Thanks for the help guys :o)

    PS I tried using the same variable name for both local and external environments but this broke the code. Hence the 2 names used.

提交回复
热议问题