How to use REG_EXPAND_SZ from the commandline?

2019-02-18 09:33发布

I was reading the Windows Commandline Documentation (Win+F1) about the commands that modify the Windows registry, particularly the the "reg add" command.

reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "%systemroot%\system32"

Now, I don't know how this was designed to work. When I invoke the command above, the variable %systemroot% gets expanded to C:\Windows. I've tried the following not to make the variable to expand, but there is no way I could force it not to:

  • escaping the `%%`'s with an `%, ^, \` - doesn't work even if I use double quotes around
  • using the single quotes '' around the whole /d string
  • use `setlocal setdelayedexpansion`? sth like:

# (setlocal enabledelayedexpansion) && (reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "!systemroot!\system32") && (setlocal disabledelayedexpansion)

The variable 'data' (/d) field is either like ^%systemroot^% or like !systemroot! or just expands to C:\windows . I could probably use the .reg file to accomplish my task, but I simply don't want to do it.
I thought that maybe there is something wrong with the program that I use to display the variable contents (regedit / regedt32 / reg query (commandline)), but after checking this is probably not the case.
Any ideas? I'm mainly interested how the variable value should look like in the regedit window, should it be like :"%systemroot%\system32" or "C:\windows\system32" to be properly expanded by other programs. Regards.

9条回答
你好瞎i
2楼-- · 2019-02-18 10:17

Please try the following:

reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot%^\system32

查看更多
劫难
3楼-- · 2019-02-18 10:21

The answer is very simple. Only quote the spaces and escape the %'s.

In command line, escape the %'s with the ^.

reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun /t REG_SZ /d title" "^%username^% /f

In batch file, escape the %'s with another %.

reg add HKLM\Software\Microsoft\Command" "Processor /v AutoRun  /t REG_SZ /d title" "%%username%% /f

This took me a while to figure out. In my mind, I should quote the entire string, but could not get the variable to be passed without expanding it. I broke my way of thinking by quoting spaces instead.

查看更多
爷的心禁止访问
4楼-- · 2019-02-18 10:25

The syntax suggested by aphoria is correct, without quotes as you discovered. But you run into trouble when you need spaces in the value.

However, it is possible to mix-in quotes in the value. It looks weird, but it works:

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d ^%SystemRoot^%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ

You can use quotes where needed, just not around your variables. And not right after a backslash.

Also, beware that the syntax for use in a batch file is not the same as on the command line. If you put that line into a batch file, you need to replace the "^" with another "%":

REG ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /d %%SystemRoot%%;c:\Program" Files"\Intel\DMIX;C:\bin /t REG_EXPAND_SZ

(cmd.exe always reminds me of Henry Spencer's quote "Those who do not understand Unix are condemned to reinvent it, poorly")

查看更多
登录 后发表回答