如何保持它采用&QUOT一个Windows批处理脚本外的变量的值;延迟扩展本地"模式? [英] How to keep the value of a variable outside a Windows batch script which uses "delayed expansion local" mode?
问题描述
上下文:我需要通过添加另一个路径来调用Windows批处理脚本,将更新我的
PATH XXX
在它的结束,但是:
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
像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 updatedPATH
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
与 CMD / V:ON
选项(激活延迟扩展,否则默认关闭),但是这是不实际的。
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
请注意使用路径串联。由于杰布 <一个href=\"http://stackoverflow.com/questions/12020152/how-to-keep-the-value-of-a-variable-outside-a-windows-batch-script-which-uses/12020153#comment16267108_12020153\">comments:
Note the concatenation of paths using. As jeb comments:
行集P =%P%;%〜2
是至关重要的,如果你的路径包含像&符号在 C:\\文件和放大器;设置
。结果
最好换将 P =;!%〜2
The line
set P=%P%;%~2
is critical if your path contains ampersands like inC:\Documents&Settings
.
Better change to set "P=!P!;%~2
".
的 SET%〜1 =%P%
是允许记忆(按 psented变量重新$ P $部分%〜1
)已使用延迟扩展等功能设置的值。结果
我最初使用 SET%〜1 =%P%!
,但杰布 <一个href=\"http://stackoverflow.com/questions/12020152/how-to-keep-the-value-of-a-variable-outside-a-windows-batch-script-which-uses/12020153#comment16267108_12020153\">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:
命令 SET%〜1 =%P%!
可以简化为 SET%〜1 =%P%
作为结尾感叹号具有延迟扩展模式,如果你prepared %p%
之前只有一个(好)的效果。
The command
SET "%~1=%P%" !
could be simplified toSET "%~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:\my\path\to\add"
和将离开该脚本,为当前DOS会话后仍然存在。
And it will persists after leaving that script, for your current DOS session.
dbenham 的回答指向一个更强大的答案(upvoted),但对我来说这个脚本已经足够了。
dbenham's answer points to a more robust answer (upvoted), but in my case this script is enough.
这篇关于如何保持它采用&QUOT一个Windows批处理脚本外的变量的值;延迟扩展本地&QUOT;模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!