echo %PATH%
Se il vostro (a livello macchina) %path%
termina con un finale \
solidus inversa (backslash), si dovrebbe raddoppiare come segue:
if "%path:~-1%"=="\" (
SETX PATH "%PATH%\"
) else (
SETX PATH "%PATH%"
)
risorse: Syntax : Escape Characters, Delimiters and Quotes (notare i miei riflessi in citazione seguente):
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\\"
Sopra dichiarazioni sul finale \
si applica allo SETX
. Ecco un esempio:
==>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\
==>
L'errore Invalid syntax
riferisce a rotture livello utente %PATH%
variabile causato utilizzando prima di SETX PATH "%PATH%"
come una variabile ambiente livello di utente ha la precedenza sul livello macchina una.Per risolvere il problema, regolare il livello di utente %PATH%
variabile (o rimuoverlo del tutto) prima via Windows GUI (preferred):
Control Panel | System | Advanced | Environment Variables
Proof - riprodurre il problema in due fasi:
Passo # 1: iniziare con buona variabile %myPath%
a livello macchina con traina \
; infine (in fine) ha rotto questa variabile per il livello utente corrente e exit
la sessione 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
Passo # 2: in una nuova sessione di cmd
, iniziare con rotta %myPath%
variabile a livello utente con trailing "
; questo solleva l'errore descritto in questione.
==>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"
==>
In realtà, io sono sicuro che termina con un ',', se non poi con la fine del nome di una cartella, ma sicuramente non con un \. Inoltre, al momento della scrittura, non esisteva una variabile 'PATH' a livello utente. – octopod