windows setx 语法无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17215562/
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-09 10:36:30  来源:igfitidea点击:

Invalid syntax with setx

windowsenvironment-variablesogre

提问by Aaron Lee

I used the setxcommand to set OGRE_HOME:

我使用setx命令来设置OGRE_HOME

setx OGRE_HOME D:\Program Files\OgreSDK

Now I need to change to value of OGRE_HOME. How can I search all the values I have set? If I run the command again, it shows that:

现在我需要更改为OGRE_HOME. 如何搜索我设置的所有值?如果我再次运行该命令,它会显示:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).

回答by ajp15243

Your path to the Ogre SDK has a space character in it, which is interpreted as a delimiter to another argument. Surround your path with "to keep it as one single argument to setx:

您到 Ogre SDK 的路径中有一个空格字符,它被解释为另一个参数的分隔符。围绕您的路径,"以将其作为单个参数setx

setx OGRE_HOME "D:\Program Files\OgreSDK"

To see the current value of the OGRE_HOMEenvironment variable:

查看OGRE_HOME环境变量的当前值:

echo %OGRE_HOME%

You may have to open a new command prompt shell to see the value if you set it and are then trying to immediately see it's value.

如果您设置了该值,然后尝试立即查看它的值,您可能需要打开一个新的命令提示符 shell 来查看该值。

To see all currently set environment variables, simply run:

要查看所有当前设置的环境变量,只需运行:

set

To show only environment variables that have a certain prefix (so FOOwould show FOOBARand FOOBAZ), put that prefix after set:

要仅显示具有特定前缀的环境变量(FOO显示FOOBARFOOBAZ),请将该前缀放在 之后set

set PREFIX

Alternatively, you can use the GUI to edit environment variables (assuming Windows 7 here).

或者,您可以使用 GUI 来编辑环境变量(此处假设为 Windows 7)。

  • Right-click Computer, choose Properties
  • Click Advanced system settingsin the left pane
  • Make sure you're on the Advancedtab in the pop-up dialog
  • Click Environment Variables...at the bottom
  • 右击Computer,选择Properties
  • Advanced system settings在左窗格中单击
  • 确保您Advanced位于弹出对话框中的选项卡上
  • 点击Environment Variables...底部

A dialog will pop up with your user-specific environment variables as well as your system-wide environment variables. Select a value and use the New/Edit/Delete buttons to interact with them.

将弹出一个对话框,其中包含用户特定的环境变量以及系统范围的环境变量。选择一个值并使用新建/编辑/删除按钮与它们交互。

回答by Gene

Command Prompt is giving you that error because you forgot the quotation marks. You should've typed:

命令提示符给你这个错误,因为你忘记了引号。你应该输入:

setx OGRE_HOME “D:\Program Files\OgreSDK”

To see all the values you've already set, enter either:

要查看您已经设置的所有值,请输入:

reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

OR

或者

reg query HKEY_CURRENT_USER\Environment

回答by Gene

setx and pretty much all windows command line commands are sensitive to certain special characters. Among them the space character but there's also the quote which is used to delimit an entry.

setx 和几乎所有 Windows 命令行命令都对某些特殊字符敏感。其中有空格字符,但也有用于分隔条目的引号。

As @ajp15243 already said, you can deal with the space by locking off the path{s) between two quotations. But what if you have paths and those path already have quotations because they carry a space? Here's an example:

正如@ajp15243 已经说过的,您可以通过锁定两个引号之间的路径来处理空格。但是,如果您有路径并且这些路径已经有引号,因为它们带有空格怎么办?下面是一个例子:

MY_PATHS="c:\Program Files\path1";"c:\Program Files(x86)\Path2"

In this case, you would have to put escape characters for those inner quotation marks when you use setx or it will get confused and give the error you listed. Eg:

在这种情况下,当您使用 setx 时,您必须为那些内部引号放置转义字符,否则它会感到困惑并给出您列出的错误。例如:

setx -m MY_PATHS "\"c:\Program Files\path1\";\"c:\Program Files(x86)\Path2\""

回答by Jthorpe

As an addendum to @ajp15243's answer. If you are doing the same with PowerShell rather than the command prompt or batch file, you'll need to call SETXwith a leading escaped double-quote character, as in:

作为@ajp15243 答案的附录。如果您使用 PowerShell 而不是命令提示符或批处理文件执行相同操作,则需要SETX使用前导转义双引号字符进行调用,如下所示:

$my_path = "%PROGRAMFILES%\MySQL\MySQL Server 5.7\bin\"
$hkcu_path = (Get-ItemProperty hkcu:\Environment).PATH + ";" + $my_path
SETX PATH "`"$hkcu_path" # note the leading escaped quote

However doing so, may result in adding a trailingdouble quote in the value of hkcu:\Environment\PATH, so you may need to do this too:

但是,这样做可能会导致在的值中添加尾随双引号hkcu:\Environment\PATH,因此您可能也需要这样做:

$dirty_path = (get-itemproperty hkcu:\Environment).PATH
$clean_path = $dirty_path -replace '"',''
SETX PATH $clean_path