当只有两个参数时,"setx" 的语法无效,因为有两个以上的参数

Invalid syntax with "setx" for having more than two arguments when there are only two arguments

我在使用 SETX 永久设置全局环境变量 PATH 时遇到问题。

我已经阅读了类似问题的答案,例如 Invalid syntax with setx as well as read the syntax on SS64

据我所知,以下行正确地遵循了 SETX 的语法并且 NOT 中的参数是否超过两个:

SETX PATH "%PATH%"

但这并不能阻止 SETX 给我这个错误:

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

上述命令是 运行 在 cmd.exe 的管理员实例上执行的。使问题更加奇怪的是 cmd.exe 的用户实例上,相同的命令完美运行。

我简化了上面的命令以强调这个问题,但我真正想要 运行 的是:

SETX PATH "%PATH%;D:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin" /M

当然,这给出了完全相同的错误,我不能在 cmd.exe 的用户实例上 运行 它,因为我正在尝试设置全局环境变量 PATH 永久。 (需要管理员权限)

说明已经在路径中了(跟setx斗了好几天,傻逼)

您可以添加相同的现有条目或添加新条目而不会出错,但不能添加不同的条目。每个用户的路径为空,每个机器的路径不为空。

 echo %PATH%

如果您的(机器级别)%path% 以尾随 \ 反斜线(反斜杠)结尾,您应该将其加倍,如下所示:

if "%path:~-1%"=="\" (
   SETX PATH "%PATH%\"
) else (
   SETX PATH "%PATH%"
)

资源:Syntax : Escape Characters, Delimiters and Quotes(请注意以下引文中我自己的亮点):

Some commands (e.g. REG and FINDSTR) use the standard escape character of \ (as used by C, Python, SQL, bash and many other languages.) The \ escape can cause problems with quoted directory paths that contain a trailing backslash because the closing quote " at the end of the line will be escaped \".

To save a directory path with a trailing backslash (\) requires adding a second backslash to 'escape the escape' so for example instead of "C:\My Docs\" use "C:\My Docs\"

以上关于尾随 \ 的陈述也适用于 SETX。这是一个例子:

==>set myPath
myPath=D:\Program Files\OgreSDK\

==>setx myOgre "%myPath%"
SUCCESS: Specified value was saved.

==>reg query HKEY_CURRENT_USER\Environment /v myOgre
HKEY_CURRENT_USER\Environment
    myOgre    REG_SZ    D:\Program Files\OgreSDK"

==>setx myOgre "%myPath%\"
SUCCESS: Specified value was saved.

==>reg query HKEY_CURRENT_USER\Environment /v myOgre
HKEY_CURRENT_USER\Environment
    myOgre    REG_SZ    D:\Program Files\OgreSDK\

==>

Invalid syntax 错误与损坏的用户级别 %PATH% 变量有关,这是由于首次使用 SETX PATH "%PATH%" 作为用户级别环境变量优先于机器级别环境变量而导致的。要解决此问题,请先调整用户级别 %PATH% 变量(或完全删除它)via Windows GUI (preferred):

Control Panel | System | Advanced | Environment Variables

证明 - 分两步重现问题

第 1 步:从良好的 %myPath% 机器级别变量开始,尾随 \;最后(很好)打破了当前用户级别和 exit cmd 会话的这个变量。

==>set myPath
myPath=D:\temp\foo foo\

==>reg query HKEY_CURRENT_USER\Environment /v myPath
ERROR: The system was unable to find the specified registry key or value.

==>reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"  /v myPath
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    myPath    REG_SZ    D:\temp\foo foo\

==>setx myPath "%myPath%;D:\temp\bu bu bu"
SUCCESS: Specified value was saved.

==>reg query HKEY_CURRENT_USER\Environment /v myPath
HKEY_CURRENT_USER\Environment
    myPath    REG_SZ    D:\temp\foo foo\;D:\temp\bu bu bu

==>rem fetch erroneous state
==>setx myPath "%myPath%"
SUCCESS: Specified value was saved.

==>reg query HKEY_CURRENT_USER\Environment /v myPath
HKEY_CURRENT_USER\Environment
    myPath    REG_SZ    D:\temp\foo foo"

==>exit

步骤 #2:在新的 cmd 会话中,从用户级别的损坏的 %myPath% 变量开始,尾随 ";这会引发问题中描述的错误。

==>set myPath
myPath=D:\temp\foo foo"

==>reg query HKEY_CURRENT_USER\Environment /v myPath
HKEY_CURRENT_USER\Environment
    myPath    REG_SZ    D:\temp\foo foo"

==>reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"  /v myPath
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
    myPath    REG_SZ    D:\temp\foo foo\

==>setx myPath "%myPath%;D:\temp\bu bu bu"
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

==>reg query HKEY_CURRENT_USER\Environment /v myPath
HKEY_CURRENT_USER\Environment
    myPath    REG_SZ    D:\temp\foo foo"

==>

以下对我有用:

第 1 步:

setx PATH "%PATH%;%GOPATH%bin"  

我收到以下回复:

WARNING: The data being saved is truncated to 1024 characters.  

SUCCESS: Specified value was saved.  

另一个更简单的例子是:

setx PATH "%PATH%;C:\Go\bin"  

第 2 步:

重启命令行,查看新的路径设置

echo %PATH%