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

I had a problem with constantly setting the global PATH environment variable using SETX .

I read answers to similar questions, such as Invalid syntax with setx , and also read SS64 syntax.

As far as I know, the following line follows the SETX syntax correctly and does NOT have more than two arguments:

 SETX PATH "%PATH%" 

But this does not stop SETX from giving me this error:

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

The above command was executed on the cmd.exe administrator instance . Even stranger, the problem is that the same command works in the user instance of cmd.exe .

I simplified the above command to emphasize the problem, but what I'm really trying to run is:

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

Of course, this gives the exact same error, and I cannot run it in a user-defined instance of cmd.exe , because I am trying to set the global PATH environment variable permenantly. (which requires administrator access)

+5
source share
3 answers
  echo %PATH% 

If your (machine level) %path% ends with a trailing backward feedback character \ (backslash), you should double it as follows:

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

Resource: Syntax: exit characters, delimiters, and quotes (note my highlights in the following quote):

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

To save the path to the directory with a backslash ( \ ), you must add a second backslash to โ€œescapeโ€, for example, instead of "C:\My Docs\" use "C:\My Docs\\"

The above instructions at the end of \ apply to SETX . Here is an example:

 ==>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\ ==> 

The Invalid syntax error refers to the broken user level variable %path% caused by the first use of SETX PATH "%PATH%" because the user level environment variable takes precedence over machine level one. To solve the problem, adjust the %path% user variable (or delete it altogether) first through the Windows GUI (preferred option) :

Control Panel | System | Advanced | Environment Variables

Proof - reproduce the problem in two steps:

Step # 1: start with a nice %myPath% variable at the machine level with the ending \ ; lastly (in fine) violated this variable for the current user level and exit cmd session.

 ==>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 

Step # 2: in a new cmd session, start with the broken variable %myPath% at the user level with the final " ; this causes the error in question.

 ==>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" ==> 
+5
source

Below worked for me:

Step 1:

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

I got the following answer:

 WARNING: The data being saved is truncated to 1024 characters. SUCCESS: Specified value was saved. 

Another simpler example might be:

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

Step 2:

Restart the command line to see the new path settings

 echo %PATH% 
+1
source

This means that it is already on the way (I fought setx several times, this is stupid).

You can add the same existing record or add a new record without errors, but not another record. The path is empty for the user, the path is not empty on the machine.

0
source

Source: https://habr.com/ru/post/1215845/


All Articles