2015-03-22 11 views
5

Ho problemi a impostare la variabile di ambiente globale PATH in modo permanente con SETX.Sintassi non valida con "setx" per avere più di due argomenti quando ci sono solo due argomenti

Ho letto le risposte a domande simili come Invalid syntax with setx nonché leggere la sintassi su SS64.

Per quanto ne so, la seguente riga segue la sintassi di SETX correttamente e fa NON hanno più di due argomenti in esso:

SETX PATH "%PATH%" 

Ma che non si ferma SETX da avermi dato questa errore:

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

Il comando precedente era corsa su un'istanza amministratore cmd.exe. Ciò che rende il problema ancora più strano è che su un'istanza utente di cmd.exe lo stesso comando funziona perfettamente.

ho semplificato il comando precedente per sottolineare il problema, ma quello che sto davvero cercando di eseguire è:

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

Naturalmente, questo dà lo stesso errore esatto, e non posso farlo funzionare su un'istanza utente di cmd.exe perché sto tentando di impostare la variabile di ambiente globale PATH in modo permanente. (che ha bisogno di accesso amministratore)

risposta

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

==> 
+0

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

0

Significa che è già nel percorso (ho combattuto setx per diversi giorni, è stupido).

È possibile aggiungere una stessa voce esistente o aggiungere una nuova voce senza errori, ma non una voce diversa. Il percorso è vuoto per utente, il percorso non è vuoto per macchina.

1

seguito ha lavorato per me:

Fase 1:

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

ho ottenuto la seguente risposta:

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

SUCCESS: Specified value was saved. 

Un altro esempio più semplice potrebbe essere:

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

Fase 2:

Riavvia la riga di comando, per vedere il nuovo percorso Opzioni

echo %PATH% 
Problemi correlati