Adding %USERPROFILE% to a command in the Windows registry

6

I'm trying to write a registry hack that will streamline some of my setup tasks when I create a new VM or repave my laptop. One thing I'm doing is switching to portable versions (synced in Dropbox) of a bunch of my favorite apps, including Notepad++.

I want to create a registry entry to allow me to have the Open with Notepad++ option in my right click menu, which is the only thing I'm missing with the portable version. I'm getting an error, though:

Windows cannot access the specified device, path or file. You may not have the 
appropriate permissions to access the item.

Here's my .reg file:

[HKEY_CLASSES_ROOT\*\shell\Open with Notepad++]
"Icon"="%USERPROFILE%\\Dropbox\\Programs\\Setup\\Icons\\Notepad++.ico"
@=""

[HKEY_CLASSES_ROOT\*\shell\Open with Notepad++\command]
@="%USERPROFILE%\\Dropbox\\Programs\\Notepad++\\notepad++.exe %1"

The icon works perfectly. I can get the entry above to work fine if I hardcode C:\Users\myusername, but I'm going for portability here.

I'm pretty sure the issue is just finding the right escape sequence for the % symbols, but it's hard to say.

Suggestions?

EDIT: I went with the add reg command option, and after a bit of trial and error, here's the command that worked:

reg add HKCR\*\shell\OpenWithNotepad++\command /t REG_EXPAND_SZ /ve /d ^%USERPROFILE^%"\Dropbox\Programs\Notepad++\notepad++.exe %1"

After running the command, I exported the results as a .reg file as well. Wins all around.

Josh Earl

Posted 2012-05-10T02:12:37.920

Reputation: 275

Answers

8

To use (expand) environment variables in the registry, the value must be of the type REG_EXPAND_SZ.

Based on this question, it would be easiest to add that kind of value with the reg command rather than a .reg file..

reg add <KEY> /v <NAME> /t REG_EXPAND_SZ /d <DATA>

See reg add /? for more information on this syntax.

You could also encode the data as hex. There's some examples of that here:

REG_EXPAND_SZ data must be presented as what MS calls a binary data type (subtype "2"), so the value must be formatted in a kind of hexidecimal format, comma-delimited, two tokens per byte (padded with zeros), with a terminating null byte of course (and further explanation is obviously far beyond the scope of this article, and the author won't be held responsible for anyone's misuse of the incomplete information given thus far). So the line above will not work but indicates the end result I wanted to achieve. As a real working .REG -file entry the example above must be rendered as:

"SoMeThIng"="%WINDIR%\\system32"

becomes

"SoMeThIng"=hex(2):22,25,57,49,4e,44,49,52,25,5c,5c,73,79,73,74,65,6d,33,32,22,00

Bob

Posted 2012-05-10T02:12:37.920

Reputation: 51 526

As crazy as it sounds, you can just put quotes around the spaces within the string. reg add HKCR\*\shell\OpenWithNotepad++\command /t REG_EXPAND_SZ /ve /d ^%USERPROFILE^%\Dropbox\Programs\Notepad++\notepad++.exe" "%1 – jaylweb – 2016-06-08T20:18:04.753

This was very helpful. Stuck on the syntax of the command, though: reg add HKCR\*\shell\Open with Notepad++\command /t REG_EXPAND_SZ /ve /d ^%USERPROFILE^%\Dropbox\Programs\Notepad++\notepad++.exe %1 is generating a syntax error. Not really sure how to handle the space before the %1, either... – Josh Earl – 2012-05-10T03:08:39.560

2Put quotes around the strings... spaces get split into separate arguments, which becomes invalid syntax (more arguments than expected). – Bob – 2012-05-10T03:10:49.807