2

I have Windows Vista, but I guess this pertains to other versions as well. I want to set a program to be used in the Open With context menu. Now I want that the path to the .exe contain an environment variable. In that way, I can move the location of my app, change the corresponding environment variable, and everything works fine.

The problem has three components:

  1. The value of the key in

    HKEY_CLASSES_ROOT\Applications\myapp.exe\shell\open\command
    

    e.g., "C:\myapp\myapp.exe" "%1", is hardcoded, and the type is REG_SZ. I would need it to be of type REG_EXPAND_SZ, e.g., "%MYAPPSDIR%\myapp.exe" "%1". This is typical for built-in programs, e.g., %SystemRoot%\system32\NOTEPAD.EXE %1.

  2. The type of key cannot be changed, so what was born as REG_SZ, so it lives.

  3. It appears that I cannot "regenerate" the variable as REG_EXPAND_SZ.

Is there any chance of doing what I mean?

There is also a bonus question, How to receive a parameter in a batch file.

1 Answer 1

3
reg add "HKCR\Applications\myapp.exe\shell\open\command" /ve /t REG_EXPAND_SZ /d "\"^%MYAPPSDIR^%\myapp.exe\" \"^%1\"" /f

Note:

  • to avoid expanding %MYAPPSDIR% by command line interpreter, % percent character should be escaped by the standard CLI escape character (^ caret): ^%MYAPPSDIR^%;
  • to include a quote mark " in the data, prefix it with the reg escape character (\ backslash). E.g. \" here is a quote as the REG command will interpret \ as an escape for the character that immediately follows it.

An example (copy&paste from my administrator command line):

C:\Windows\system32>reg query "HKCR\Applications\mspaint.exe\shell\edit\command" /ve

HKEY_CLASSES_ROOT\Applications\mspaint.exe\shell\edit\command
    (Default)    REG_EXPAND_SZ    "%systemroot%\system32\mspaint.exe" "%1"

C:\Windows\system32>reg add "HKCR\Applications\mspaint.exe\shell\edit\command" /ve /t
 REG_EXPAND_SZ /d "\"^%systemroot^%\system32\mspaint.exe\" \"^%1\"" /f
The operation completed successfully.

C:\Windows\system32>reg query "HKCR\Applications\mspaint.exe\shell\edit\command" /ve

HKEY_CLASSES_ROOT\Applications\mspaint.exe\shell\edit\command
    (Default)    REG_EXPAND_SZ    "%systemroot%\system32\mspaint.exe" "%1"
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .