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