How to append a registry key value that has spaces in it?

1

I found the following page (How do I modify the data of an existing registry key value name from cmd?) and it was extermely helpful, but I have a further question.

My PATH has a value that includes "c:\Program Files\Microsoft SQL Server\100\Tools\Binn\", and I'd like to add "P:\SQL" to it. When I run the script below, my PATH will then look like "C:\Program;P:\SQL". What needs to be modified so my PATH will look like "c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;P:\SQL"?

Here is what I have:

for /F "skip=2 tokens=3" %%r in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set oldVal=%%r    
echo previous=%oldVal%    
set newVal=%oldVal%;P:SQL    
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_EXPAND_SZ /d %newVal% /f

Thanks.

Nick H

Posted 2016-06-27T17:38:13.667

Reputation: 13

Use /v not /ve to start with. – DavidPostill – 2016-06-27T17:52:48.543

Why are you using the registry to set the path? – DavidPostill – 2016-06-27T17:55:39.467

DavidPostill, I had tried with only '/V' but it didn't work either.

We are deploying a new software to 700 computers and I need to add P:\SQL to the PATH for modules of that app to access data, and I don't think that sending a tech to 700 computers is worthwhile. – Nick H – 2016-06-27T18:00:29.803

Answers

1

How do I add a value to my PATH?

My PATH has a value that includes "c:\Program Files\Microsoft SQL Server\100\Tools\Binn\", and I'd like to add "P:\SQL" to it

Why are you reading/writing the registry?

Just use setx to add a value to your PATH:

setx PATH "%PATH%;P:\SQL" /m

Note:

  • /m Set the variable in the system environment HKLM.

    (The default is the local environment HKCU)


Further Reading


DavidPostill

Posted 2016-06-27T17:38:13.667

Reputation: 118 938

Why I was using the registry? Because I know just enough to get myself in trouble. :)

You fixed the issue. Thank you – Nick H – 2016-06-27T19:32:07.227

1

If you really want to tamper the registry you do so the following, but at your own risk

@echo off
set new_value=;P:/SQL     ::set the new value here

set key="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
for /F "delims= skip=2" %%r in ('reg query %key% /v "Path"') do set old_value=%%r
set old_value=%old_value:    Path    REG_SZ    =%
set new_value=%old_value%%new_value%
reg add %key% /v "Path" /d "%new_value%" /f

I must say, this is rather very crude way to do this. Always remember to take a backup of the registry before altering it.

Michael D

Posted 2016-06-27T17:38:13.667

Reputation: 717