是的,REG ADD
会覆盖现有值,但我写了一个脚本,应该可以帮助你。
请注意后面的字符delims=
:这些不是空格,它是一个制表符(ASCII #9)。
@ECHO OFF
REM Declare all variables as local.
SETLOCAL
SET GET_PATH=REG QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path
SET SET_PATH=REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /f /d
REM Get %PATH% from registry and filter REG output (drop all but actual %PATH% value).
FOR /F "tokens=3* delims= " %%P IN ('%GET_PATH% ^| FINDSTR Path') DO SET RESULT=%%P
REM Check for the trailing semicolon.
IF "%RESULT:~-1%" == ";" (SET SEMICOLON=) ELSE (SET SEMICOLON=;)
REM Add new directories to %PATH%.
SET DIRS=C:\Foo\Bar;E:\Bar\Baz
SET RESULT=%RESULT%%SEMICOLON%%DIRS%
REM Save result to the registry.
%SET_PATH% "%RESULT%"