本文介绍了如何将变量的值保留在使用“延迟扩展本地”的Windows批处理脚本之外模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

上下文:我需要调用Windows批处理脚本,通过添加另一个路径' xxx来更新我的 PATH


  • 没有任何重复

    (if我添加' xxx '到PATH,像' aaa; xxx; bbb ',我需要一个更新的 PATH 喜欢' aaa; bbb; xxx ')

  • 没有任何聚合

    (我可以重复地调用脚本,而不用' aaa; bbb; xxx; xxx; xxx; ... ')



  • 我尝试过:



    以下函数负责重复任务, / p>

     :cleanAddPath  - 从PATH中删除%〜1,将其添加到PATH结尾
    SETLOCAL ENABLEDELAYEDEXPANSION
    设置PATH =!PATH:%〜2 =!
    set PATH =!PATH:;; =;!
    设置PATH =%PATH%;%〜2
    设置P =!P:;; =
    echo%PATH%
    echo -------------
    ENDLOCAL
    退出/ b
    / pre>

    但是,它需要,这意味着:在脚本结束(或这里,函数结束时 cleanAddPath ),任何为%PATH%设置的东西被抛弃



    我可以问用户为此,您可以使用 cmd / V:ON 选项启动 cmd (激活延迟扩展,否则关闭默认),但这是不实际的。



    如何修改 PATH 变量的方式我在上面描述过,并且在调用脚本后,在我当前的DOS会话中仍然有更新

    解决方案

    页面 提供了一个函数如何在DOS中返回值的很好的例子,即使使用模式:



    以下函数将使用添加 PATH 更新所需的任何变量:

     :cleanAddPath  - 从%〜1中删除%〜2,将其添加到%〜1 
    的结尾SETLOCAL ENABLEDELAYEDEXPANSION
    设置P =!%〜1!
    设置P =!P:%〜2 =!
    设置P =!P:;; =;!
    设置P =!P!;%〜2
    设置P =!P:;; =
    (ENDLOCAL& REM .-- RETURN VALUES
    SET%〜1 =%P%

    退出/ b

    注意使用的路径的并置。作为 :

    SET%〜1 =%P%是允许记住(使用%〜1 表示的变量)您使用延迟扩展功能设置的值。

    我最初使用 SET%〜1 =%P%!,但 :

    要更新您的 PATH 变量,您可以调用您的功能与:

     调用:cleanAddPath PATHC:\my\path\to\add

    在离开该脚本后,将持续存在您当前的DOS会话。



    的指出一个更强大的答案(upvoted),但在我的情况下,这个脚本就够了。


    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:

    • without any duplicate
      (if I add 'xxx' to a PATH like 'aaa;xxx;bbb', I need an updated PATH like 'aaa;bbb;xxx')
    • without any aggregation
      (I can call the script repeatedly without ending up with 'aaa;bbb;xxx;xxx;xxx;...')

    What I have tried:

    The following function takes care of any duplicate and does the job

    :cleanAddPath -- remove %~1 from PATH, add it at the end of PATH
    SETLOCAL ENABLEDELAYEDEXPANSION
    set PATH=!PATH:%~2=!
    set PATH=!PATH:;;=;!
    set PATH=%PATH%;%~2
    set P=!P:;;=;!
    echo %PATH%
    echo -------------
    ENDLOCAL
    exit /b
    

    But, it needs delayed expansion local mode, which means: at the end of the script (or here, at the end of the function cleanAddPath), whatever has been set for %PATH% is thrown away.

    I could ask the users (for which I write the script) to launch their cmd with a cmd /V:ON option (activating the delayed expansion, otherwise off by default), but that is not practical.

    How can I modify the PATH variable the way I described above, and still have it updated in my current DOS session after calling said script?

    解决方案

    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 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:

    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.

    这篇关于如何将变量的值保留在使用“延迟扩展本地”的Windows批处理脚本之外模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    1403页,肝出来的..

    09-06 10:24