echo %PATH%
If your (machine level) %path%
ends with a trailing \
reverse solidus (backslash), you should double it as follows:
if "%path:~-1%"=="\" (
SETX PATH "%PATH%\"
) else (
SETX PATH "%PATH%"
)
Resource: Syntax : Escape Characters, Delimiters and Quotes (note my own highlights in following citation):
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\\"
Above statements on trailing \
applies to SETX
as well. Here's 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 relates to broken user level %PATH%
variable caused by first using of SETX PATH "%PATH%"
as user level environment variable takes precedence over machine level one. To solve the problem, adjust user level %PATH%
variable (or remove it at all) first via Windows GUI (preferred):
Control Panel | System | Advanced | Environment Variables
Proof - reproduce the problem in two steps:
Step #1: start with good %myPath%
variable on machine level with trailing \
; lastly (in fine) broke this variable for current user level and exit
the 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 broken %myPath%
variable on user level with trailing "
; this raises the error described 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"
==>
;
, if not then with the end of a folder name, but definitely not with a \. Also, at the time of writing, there didn't exist aPATH
variable on the user level. – Honorific