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

后端 未结 3 1601
悲&欢浪女
悲&欢浪女 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:19

    The problem isn't as simple as you think. There are a number of issues that can break your code before it ever gets to the end where it needs to return the updated value across the ENDLOCAL barrier.

    I already answered this question as an extension to an answer I provided for a similar question. See How to check if directory exists in %PATH%?. In that answer I provide a large list of issues that complicate the problem.
    The code at the bottom of the linked answer shows how to reliably add a path if it does not exist in PATH already, and it also demonstrates how to reliably return the value across the ENDLOCAL barrier.

    The following edits are from VonC in an attempt to actually put the answer here instead of just a link to the answer. I'll preserve the edit, but I find it difficult to follow without the context of the full linked answer.

    [The answer demonstrates how to reliably return the value] using the set "%~1=%var%" ! trick (with the trailing '!')

    That thread includes:

    That's not clear to me. How can an exclamation mark behind the last quote influence the variable content?

    The simple rule for delayed expansion is:
    For each character in the line do:

    • If it is a caret (^) the next character has no special meaning, the caret itself is removed
    • If it is an exclamation mark, search for the next exclamation mark (carets are not observed here), then expands to the content of the variable
    • If no exclamation mark is found in this phase, the result is discarded, the result of the phase before is used instead (important for the carets)

    So, at this point the difference should be clear, the carets are removed even if the exclamation mark have no other effect in a line.
    Example:

    @echo off
    setlocal EnableDelayedExpansion
    
    echo one caret^^
    echo none caret^^  !
    
    set "var1=one caret^"
    set "var2=none caret^" !
    
    echo !var1!
    echo !var2!
    ----- OUTPUT ----
    one caret^
    none caret
    one caret^
    one caret
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-10 05:31

    The page "DOS - Function Collection" gives great example on how a function can return a value in DOS, even when using delayed expansion mode:

    The following function will update any variable you want with an addition PATH:

    :cleanAddPath -- remove %~2 from %~1, add it at the end of %~1
    SETLOCAL ENABLEDELAYEDEXPANSION
    set P=!%~1!
    set P=!P:%~2=!
    set P=!P:;;=;!
    set P=!P!;%~2
    set P=!P:;;=;!
    (ENDLOCAL & REM.-- RETURN VALUES
      SET "%~1=%P%"
    )
    exit /b
    

    Note the concatenation of paths using. As jeb comments:

    The line set P=%P%;%~2 is critical if your path contains ampersands like in C:\Documents&Settings.
    Better change to set "P=!P!;%~2".

    The SET "%~1=%P%" is the part which allows to memorize (in the variable represented by %~1) the value you have set using delayed expansion features.
    I initially used SET "%~1=%P%" !, but jeb comments:

    The command SET "%~1=%P%" ! could be simplified to SET "%~1=%P%" as the trailing exclamation mark has only a (good) effect in delayed expansion mode and if you prepared %P% before.

    To update your PATH variable, you would call your function with:

    call :cleanAddPath PATH "C:\my\path\to\add"
    

    And it will persists after leaving that script, for your current DOS session.

    dbenham's answer points to a more robust answer (upvoted), but in my case this script is enough.

    0 讨论(0)
提交回复
热议问题