0

I want to expand the Path registry variable's value with a value of a batch file variable

:: This is the content of the install.cmd
set ROOT_DIR=%~dp0%
echo %path%|find /i "%ROOT_DIR%\Tools\">nul  || set path=%path%;%ROOT_DIR%\Tools\
reg.exe ADD "HKEY_CURRENT_USER\Environment" /v Path /t REG_EXPAND_SZ /d %path% /f

Expected: it appends the %ROOT_DIR%\Tools to the %path% if it does not contain already, then write out the whole path to the Path Registry entry

Actual: Invalid syntax


Variation 1

reg.exe ADD "HKEY_CURRENT_USER\Environment" /v Path /t REG_EXPAND_SZ /d ^%path^% /f

Result: it ignores the force switch, remove everything from the Path and overwrite it with a '/f' string


Variation 2

reg.exe ADD "HKEY_CURRENT_USER\Environment" /v Path /t REG_EXPAND_SZ /d %%path%% /f

Result: it overrides the content of the Path registry entry with a "%path%" string (not with its value)


Variation 3

reg.exe ADD "HKEY_CURRENT_USER\Environment" /v Path /t REG_EXPAND_SZ /d path /f

Result: it overrides the content of the Path registry entry with a "path" string (not with its value)

  • I'd try using `setx` instead – Magoo Jun 15 '19 at 22:53
  • 3
    `echo %path%` to `find` seems OK. If you update `HKCU\Environment /v path`, you should read from `HKCU\Environment /v path`, extend with your dir path, and write back to `HKCU\Environment /v path`. Use of `%path%` to extend and write is a combination of both `HKCU\Environment /v path` and `HKLM\Environment /v path` and your dir path, which can create duplicates in `%path%`. Once `%path%` exceeds a certain length, it can trunicate, which can cause unexpected behavior. – michael_heath Jun 16 '19 at 04:37
  • 1
    Related: [adding directory to path environment variable](https://stackoverflow.com/q/42140086/3439404). – JosefZ Jun 16 '19 at 10:13
  • 1
    See [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) and [How to search and replace a string in environment variable PATH?](https://stackoverflow.com/a/24650324/3074564) and [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564). – Mofi Jun 16 '19 at 10:59

0 Answers0