Windows 命令行:不评估环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/323426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:39:45  来源:igfitidea点击:

Windows Command Line: Non Evaluation of Environment Variable

windowscommand-linebiztalkenvironment-variables

提问by marcj

I would like to provide the raw text referring to an environment variable to a command instead of evaluating the environment variable.

我想将引用环境变量的原始文本提供给命令,而不是评估环境变量。

I need this to configure BizTalk from the command line, for example:

我需要这个来从命令行配置 BizTalk,例如:

BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:%BTAD_InstallDir%\App1.Schemas.dll

BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:%BTAD_InstallDir%\App1.Schemas.dll

This command adds a resource to a BizTalk application. I want the destination to be %BTAD_InstallDir%\App1.Schemas.dll, however at present it is evaluating the environment variable (to nothing) and using \App1.Schemas.dll.

此命令将资源添加到 BizTalk 应用程序。我希望目标是 %BTAD_InstallDir%\App1.Schemas.dll,但是目前它正在评估环境变量(无)并使用 \App1.Schemas.dll。

Is it possible to escape or disable the evaluation of this environment variable while parsing\executing this command?

在解析\执行此命令时是否可以转义或禁用此环境变量的评估?

I have tried escaping the first and both percentage characters with a carrot (^), however this did not stop the evaluation.

我曾尝试用胡萝卜 (^) 转义第一个和两个百分比字符,但这并没有停止评估。

[EDIT]When I execute this at the command prompt it doesn't replace the environment variable, however it does when I run it as a script, any thoughts as to why this is different?

[编辑]当我在命令提示符下执行它时,它不会替换环境变量,但是当我将它作为脚本运行时会替换,关于为什么会有所不同的任何想法?

采纳答案by VonC

Did you try:

你试过了吗:

%%BTAD_InstallDir%%

in your script ?

在你的脚本中?

That should prevent the script to interpret the variable, and it would pass %BTAD_InstallDir%to the program.

这应该会阻止脚本解释变量,它会传递%BTAD_InstallDir%给程序。

回答by Autodidact

Try echo ^%path^% in a command prompt it prints...

在它打印的命令提示符中尝试 echo ^%path^% ...

path

小路

instead of expanding the environment variable so I guess the following should work for you as suggested by Mikeage

而不是扩展环境变量,所以我想以下内容应该适用于 Mikeage 的建议

BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:^%BTAD_InstallDir^%\App1.Schemas.dll

BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:^%BTAD_InstallDir^%\App1.Schemas.dll

回答by Mikeage

Try ^% instead of %.

尝试 ^% 而不是 %。

回答by PhiLho

Tried:

尝试过:

C:\PrgCmdLine\Unix\echo.exe "%"JAVA_HOME"%"

Got:

得到了:

%JAVA_HOME%

[EDIT] Indeed, C:\PrgCmdLine\Unix\echo.exe ^%JAVA_HOME^%works too, and is simpler...

[编辑] 确实,C:\PrgCmdLine\Unix\echo.exe ^%JAVA_HOME^%也能用,而且更简单……

[EDIT 2] For the record: I used UnxUtils' echo to have the behavior of a plain program. Built in echo has a slightly different behavior, at least for quoted % signs.

[编辑 2] 作为记录:我使用 UnxUtils 的 echo 来获得普通程序的行为。内置 echo 的行为略有不同,至少对于引用的 % 符号。

回答by Kristof

Not sure if it's the same as my case, but i was troubling to use a batch file to create a script which has %temp% variable inside. The workaround i found: set test=%temp; echo {command} %test%%>>path_to_my_batch_file; Hope this helps someone:)

不确定它是否与我的情况相同,但我很难使用批处理文件来创建一个内部包含 %temp% 变量的脚本。我发现的解决方法:set test=%temp; 回声 {命令} %test%%>>path_to_my_batch_file; 希望这对某人有帮助:)