如何将变量的值保留在使用“本地延迟扩展"的 Windows 批处理脚本之外.模式? [英] How to keep the value of a variable outside a Windows batch script which uses "delayed expansion local" mode?

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

问题描述

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

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:

  • 没有任何重复
    (如果我将 'xxx' 添加到 'aaa;xxx;bbb' 之类的 PATH 中,我需要一个更新的 PATH 之类的 'aaa;bbb;xxx')
  • 没有任何聚合
    (我可以重复调用脚本,而不会以 'aaa;bbb;xxx;xxx;xxx;...' 结束)
  • 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;...')

我尝试过的:

以下函数负责处理任何重复并完成工作

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

但是,它需要延迟扩展本地模式,也就是说: 在脚本的末尾(或者这里,在函数 cleanAddPath 的末尾),%PATH% 设置的任何内容都会被丢弃.

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.

我可以要求用户(我为此编写脚本)使用 cmd/V:ON 选项启动他们的 cmd(激活延迟扩展,否则关闭默认情况下),但这并不实用.

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.

如何按照我上面描述的方式修改 PATH 变量,并且在调用所述脚本之后 在我当前的 DOS 会话中更新它?

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?

推荐答案

页面DOS - 函数集合" 给出了一个很好的例子,说明一个函数如何在 DOS 中返回一个值,即使使用 延迟扩展 模式:

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

以下函数将通过添加 PATH 来更新您想要的任何变量:

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

注意路径的串联使用.作为 jeb 评论:

Note the concatenation of paths using. As jeb comments:

如果您的路径包含像 C:Documents&Settings 中的 & 符号,则 set P=%P%;%~2 行非常重要.
最好更改设置P=!P!;%~2".

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".

SET "%~1=%P%" 是允许记住(在由%~1 表示的变量中)您设置的值的部分使用延迟扩展功能.
我最初使用 SET "%~1=%P%" !,但 jeb 评论:

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:

命令 SET "%~1=%P%" ! 可以简化为 SET "%~1=%P%",因为后面的感叹号有仅在延迟扩展模式下(如果您之前准备过 %P%)效果(良好).

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.

要更新你的 PATH 变量,你可以调用你的函数:

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

call :cleanAddPath PATH "C:mypath	oadd"

在离开该脚本后,对于您当前的 DOS 会话,它将持续存在.

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

dbenhamanswer 指向一个更可靠的答案(赞成),但在我的情况下,这个脚本就足够了.

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆