使一个环境变量生存ENDLOCAL [英] Make an environment variable survive ENDLOCAL

查看:139
本文介绍了使一个环境变量生存ENDLOCAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过一系列中间变量计算变量的批处理文件:

I have a batch file that computes a variable via a series of intermediate variables:

@echo off
setlocal

set base=compute directory
set pkg=compute sub-directory
set scripts=%base%\%pkg%\Scripts

endlocal

%scripts%\activate.bat

在最后一行的脚本不被调用,因为它是ENDLOCAL,这则会覆盖了脚本环境变量之后,但它要来的endlocal因为它的目的后,是设置了一堆其他的环境变量供用户使用。

The script on the last line isn't called, because it comes after endlocal, which clobbers the scripts environment variable, but it has to come after endlocal because its purpose is to set a bunch of other environment variables for use by the user.

我如何调用脚本谁的目的是建立永久性的环境变量,但谁的位置是由一个临时环境变量确定?

我知道我可以前的endlocal创建一个临时批处理文件,并经过ENDLOCAL,而如果没有其他涉及到光我会做调用它,但我想知道是否有一个不太畏缩值得的解决方案。

I know I can create a temporary batch file before endlocal and call it after endlocal, which I will do if nothing else comes to light, but I would like to know if there is a less cringe-worthy solution.

推荐答案

ENDLOCAL&安培; SET VAR =%的TempVar%模式是经典之作。但在有些情况下是不理想的。

The ENDLOCAL & SET VAR=%TEMPVAR% pattern is classic. But there are situations where it is not ideal.

如果您不知道的TempVar的内容,那么你可能会如果值包含特殊字符,如&LT遇到的问题; > &安培; | 。您通常可以防止通过使用引号,比如 SETVAR =%的TempVar%,但如果有特殊字符和值已经引用可能会导致问题。

If you do not know the contents of TEMPVAR, then you might run into problems if the value contains special characters like < > & or|. You can generally protect against that by using quotes like SET "VAR=%TEMPVAR%", but that can cause problems if there are special characters and the value is already quoted.

有关前pression A是跨越障碍ENDLOCAL输送了价值,如果你关心的特殊字符的绝佳选择。延迟扩展应在ENDLOCAL前ENDLOCAL后启用或禁用。

A FOR expression is an excellent choice to transport a value across the ENDLOCAL barrier if you are concerned about special characters. Delayed expansion should be enabled before the ENDLOCAL, and disabled after the ENDLOCAL.

setlocal enableDelayedExpansion
set "TEMPVAR=This & "that ^& the other thing"
for /f "delims=" %%A in (""!TEMPVAR!"") do endlocal & set "VAR=%%~A"

限制:


  • 如果延迟扩展的ENDLOCAL后启用,那么最终值将如果TempVar的包含损坏

值不能运

如果您必须返回多个值,你知道,不能出现在任何一个值,然后只需使用FOR / F选择适当的字符。例如,如果我知道的值不能包含 |

If you must return multiple values, and you know of a character that cannot appear in either value, then simply use the appropriate FOR /F options. For example, if I know that the values cannot contain |:

setlocal enableDelayedExpansion
set "temp1=val1"
set "temp2=val2"
for /f "tokens=1,2 delims=|" %%A in (""!temp1!"|"!temp2!"") do (
   endLocal
   set "var1=%%~A"
   set "var2=%%~B"
)

如果您必须返回多个值,字符集是不受限制的,那么使用嵌套FOR / F循环:

If you must return multiple values, and the character set is unrestricted, then use nested FOR /F loops:

setlocal enableDelayedExpansion
set "temp1=val1"
set "temp2=val2"
for /f "delims=" %%A in (""!temp1!"") do (
  for /f "delims=" %%B in (""!temp2!"") do (
    endlocal
    set "var1=%%~A"
    set "var2=%%~B"
  )
)

绝对退房杰布的回答一个安全,防弹技术,在所有情况下的所有可能值的作品。

Definitely check out jeb's answer for a safe, bullet proof technique that works for all possible values in all situations.

这篇关于使一个环境变量生存ENDLOCAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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