问题描述
上下文:我需要调用Windows批处理脚本,通过添加另一个路径' 我尝试过: 以下函数负责重复任务, / p> 但是,它需要,这意味着:在脚本结束(或这里,函数结束时 我可以问用户为此,您可以使用 如何修改 页面 提供了一个函数如何在DOS中返回值的很好的例子,即使使用模式: 以下函数将使用添加 注意使用的路径的并置。作为 : 要更新您的 在离开该脚本后,将持续存在您当前的DOS会话。 的指出一个更强大的答案(upvoted),但在我的情况下,这个脚本就够了。 Context: I need to call a Windows batch script which would update my What I have tried: The following function takes care of any duplicate and does the job But, it needs delayed expansion local mode, which means: at the end of the script (or here, at the end of the function I could ask the users (for which I write the script) to launch their How can I modify the 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 Note the concatenation of paths using. As jeb comments: The To update your 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批处理脚本之外模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! xxx来更新我的
PATH
$>
(if我添加' xxx
'到PATH,像' aaa; xxx; bbb
',我需要一个更新的 PATH
喜欢' aaa; bbb; xxx
')
(我可以重复地调用脚本,而不用' aaa; bbb; xxx; xxx; xxx; ...
')
:cleanAddPath - 从PATH中删除%〜1,将其添加到PATH结尾
/ pre>
SETLOCAL ENABLEDELAYEDEXPANSION
设置PATH =!PATH:%〜2 =!
set PATH =!PATH:;; =;!
设置PATH =%PATH%;%〜2
设置P =!P:;; =
echo%PATH%
echo -------------
ENDLOCAL
退出/ b
cleanAddPath
),任何为%PATH%
设置的东西被抛弃。
cmd / V:ON
选项启动 cmd
(激活延迟扩展,否则关闭默认),但这是不实际的。
PATH
变量的方式我在上面描述过,并且在调用脚本后,在我当前的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
PATH
by adding another path 'xxx
' at the end of it, but:
(if I add 'xxx
' to a PATH like 'aaa;xxx;bbb
', I need an updated PATH
like 'aaa;bbb;xxx
')
(I can call the script repeatedly without ending up with 'aaa;bbb;xxx;xxx;xxx;...
'):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
), whatever has been set for %PATH%
is thrown away.cmd
with a cmd /V:ON
option (activating the delayed expansion, otherwise off by default), but that is not practical.PATH
variable the way I described above, and still have it updated in my current DOS session after calling said script?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
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:PATH
variable, you would call your function with:call :cleanAddPath PATH "C:\my\path\to\add"