如何设置环境变量与空间? [英] How to set environment variables with spaces?

查看:214
本文介绍了如何设置环境变量与空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要值设置使用批处理文件中的环境变量。我写的脚本是:

I need to set values to a environmental variable using a batch file. I wrote the script for this:

@echo off
set value="Hello world"
setx -M srijani "%srijani%;%value%"

它给人的错误:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

我用Google搜索,并发现同时使用空格,我们需要把它写一个双引号。

I googled and found that while using white spaces we need to write it inside a double quotes.

set value="Hello world"

但是,这是不是工作压力太大。

But, that is not working too.

请注意:我在Windows 7

Note: I am on Windows 7.

推荐答案

通过命令的输出误差 SETX 由报价错误的使用情况分配字符串变量引起的

The error output by command setx is caused by wrong usage of the quotes on assigning the string to variable value.

该命令的设置并参数是变量=值。对于大多数命令和应用程序很可能和经常需要使用双引号的参数,如果含有1个或多个空格或其他字符从这个名单:及()[] {} ^ =; !'+,`〜。这些字符是通过在命令提示符窗口中运行显示在上次帮助页面输出 CMD /?帮助CMD

The command is set and the parameter is variable=value. As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &()[]{}^=;!'+,`~. Those characters are displayed on last help page output by running in a command prompt window cmd /? or help cmd.

但是,错误是在这里:

set value="Hello world"

随着等号变量=值命令的全部参数后首次双引号的设置不是双引号括起来。

With first double quote after the equal sign the entire parameter variable=value of command set is not enclosed in double quotes.

这导致跨preting双引号作为字符串的一部分分配给变量名称为。从等号线包括双引号的结束,可能存在尾随空格和水平制表符一切都在这里赋给变量,而不只是字符串世界,你好预期。

This results in interpreting the double quotes as part of the string to assign to variable with name value. Everything from the equal sign to end of line including the double quotes and possibly existing trailing spaces and horizontal tabs is assigned here to variable value instead of just the string Hello world as expected.

在扩展行

setx -M srijani "%srijani%;%value%"

因此​​,结果是:

the result is therefore:

setx -M srijani "Value of variable srijani;"Hello world""

和命令的 SETX 间$ P $点错引用参数作为语法错误。

And command setx interprets the wrong quoted parameter as syntax error.

正确将使用:

set "value=Hello world"

现在命令的整个参数的设置被双引号括起来。因此忽视了解析系列有:

Now the entire parameter of command set is enclosed in double quotes. Therefore ignored on parsing the line are:


  • 所有的空格/命令与标签的设置并首次双引号,

  • 第一个双引号,

  • 最后的双引号,

  • 和所有现存或许空间/最后的双引号后的标签。

  • all spaces/tabs between command set and the first double quote,
  • the first double quote,
  • the last double quote,
  • and all perhaps existing spaces/tabs after last double quote.

因此​​,只要世界,你好被分配给一个变​​量名称为

So just Hello world is assigned to a variable with name value.

有关字符串的正确赋值给一个环境变量的更多细节为什么是回声%VAR%无串输出在命令行设置VAR =文字''使用后'?的它还包含了一个简单的演示一批code。

For more details about correct assignment of a string to an environment variable read answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It contains also a simple demo batch code.

一些详细信息:

含有1或更多的报价介于中间的一个参数字符串是如何跨preTED取决于命令分别应用。 pting参数与1个或多个于跨$ P $行为HTTP参数字符串中可以根据使用的编译器作为的批处理文件:特定文件夹列表RAR文件,并写入结果放入文本文件当然命令/应用程序的源$ C ​​$ C

How an argument string containing 1 or more quotes somewhere in the middle is interpreted depends on command respectively application. The behavior on interpreting an argument with 1 or more " within an argument string can vary depending on used compiler as explained in an answer on batch file: list rar file in specific folder and write result into text file and of course the source code of the command / application.

对于大多数命令和应用程序的正确的语法是:

For most commands and applications the correct syntax is:

command "parameter in quotes"
"Path to application\app.exe" "parameter in quotes" 

但也有它需要一个参数字符串中间报价申请。这样的应用的一个例子是的 Windows资源管理器

下面的语法需要打开的浏览器的距离,在窗口中显示当前目录中的批处理文件中的窗口。

The following syntax is required to open an Explorer window from within a batch file with current directory displayed in window.

explorer.exe /e,"%CD%"

不工作是:

explorer.exe "/e,%CD%"
explorer.exe /e "%CD%"

所以的Explorer.exe 要求在 / E 指定在中间引号打开的目录参数字符串或者跨$ p $点的/ E,%CD%分别为/ E%CD%与路径显示在目录名的浏览器的窗口。

So explorer.exe requires that the directory to open is specified after /e, with quotes in the middle of parameter string or it interprets "/e,%CD%" respectively "/e %CD%" as name of the directory with path to display in Explorer window.

另请参阅 Windows资源管理器的命令行选项


  • KB130510 (Windows 95中,视窗98,视窗千年时,Windows NT 4.0 )

  • KB152457 (的Windows NT 4.0,Windows 2000中)

  • KB314853 (Windows XP中)

  • KB130510 (Windows 95, Windows 98, Windows Millennium, Windows NT 4.0)
  • KB152457 (Windows NT 4.0, Windows 2000)
  • KB314853 (Windows XP)

Windows资源管理器的命令行选项记录那些Microsoft支持文章,正对所有3篇文章都一样还连续工作的浏览器的Windows Server 2003上,Windows Vista中,在Windows Server 2008 R2,Windows 7中,Windows Server 2012中R2的Windows 8.0,Windows 8.1中时,Windows 10,换句话说,所有32位和64位Windows。

The Windows Explorer command line options documented on those Microsoft support articles and being all the same on all 3 articles work also for Explorer on Windows Server 2003, Windows Vista, Windows Server 2008 R2, Windows 7, Windows Server 2012 R2, Windows 8.0, Windows 8.1, Windows 10, in other words for all 32-bit and 64-bit Windows.

这篇关于如何设置环境变量与空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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