echo %PATH%
Nếu (cấp máy) của bạn %path%
kết thúc với một trailing \
gạch chéo ở cuối đảo ngược (xuyệc ngược), bạn nên tăng gấp đôi nó như sau:
if "%path:~-1%"=="\" (
SETX PATH "%PATH%\"
) else (
SETX PATH "%PATH%"
)
Resource: Syntax : Escape Characters, Delimiters and Quotes (lưu ý điểm nổi bật của riêng tôi trong trích dẫn sau đây):
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\\"
tuyên bố trên vào trailing \
áp dụng cho SETX
là tốt. Dưới đây là một ví dụ:
==>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\
==>
Các lỗi Invalid syntax
liên quan đến phá vỡ mức người dùng %PATH%
biến do đầu tiên sử dụng của SETX PATH "%PATH%"
như biến môi trường cấp độ người dùng được ưu tiên hơn mức máy một.Để giải quyết vấn đề, điều chỉnh mức sử dụng %PATH%
biến (hoặc loại bỏ nó ở tất cả) đầu tiên via Windows GUI (preferred):
Control Panel | System | Advanced | Environment Variables
Proof - tạo lại vấn đề theo hai bước:
BướC# 1: bắt đầu với tốt %myPath%
biến trên mức máy với đường nhỏ \
; cuối cùng (bằng tiền phạt) đã phá vỡ biến này cho cấp người dùng hiện tại và exit
phiên 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
BướC# 2: trong một phiên làm việc mới cmd
, bắt đầu với tấm %myPath%
biến trên cấp người dùng với trailing "
; điều này làm tăng lỗi được mô tả trong câu hỏi.
==>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"
==>
Thực ra, tôi chắc chắn rằng nó kết thúc bằng một ';', nếu không thì với kết thúc của một tên thư mục, nhưng chắc chắn không phải với một \. Ngoài ra, tại thời điểm viết, không tồn tại biến 'PATH' ở cấp độ người dùng. – octopod