add windows system %path% variable, commad line

The idea is update the path environment via command line:
I can not use %path% variable cause it reports %systemroot% as c:\windows Like this code. It do the trick but it is wrong:
set add=c:\tools
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" ^
/v Path /t REG_EXPAND_SZ /d "%path%;%add%" /f
I have to play with:
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

When i query from XP it reports:
When i query Windows 7 it reports:
I start to make work around with reg query.

Folowing code needs:

This solution included finding duplicate entry and do not let you add existing path variable:
Some explanations:
sed "s/! REG.EXE VERSION 3.0//I" deletes first founded "! REG.EXE VERSION 3.0". This is needed to support Windows XP. I - means not case sensitive

sed "s/Path//I" deletes first founded "path"

sed "s/REG_EXPAND_SZ//I" deletes first founded "REG_EXPAND_SZ"

sed "s/HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment//I" deletes first founded
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

sed s/^[ \t]*//g" deletes all tabs and white spaces at the start of the line

sed "/./!d" removes all empty lines.

sed "$a;%p%" finally adds new variable

sed "s/;/\n/g" substitute semicolon with new line character. It splits all variables into colon

awk "!a[$0]++" since the is each variable on single line we can check for duplicate entry and remove it.

sed "/./!d" - if something was found dublicated then now it is removed and there is one line witch contains nothing and this line must me removed.

sed ":a;N;$!ba;s/\n/;/g" finally we substitute back again new line character with semicolon :)

Note:
The set p= variable must contains path with double backslashes like
C:\\Program Files (x86)\\GnuWin32\\bin

No comments: