windows 如何从命令行使用 REG_EXPAND_SZ?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3620388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use REG_EXPAND_SZ from the commandline?
提问by colemik
I was reading the Windows Commandline Documentation (Win+F1) about the commands that modify the Windows registry, particularly the the "reg add" command.
我正在阅读有关修改 Windows 注册表的命令的 Windows 命令行文档 (Win+F1),尤其是“reg add”命令。
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:
现在,我不知道这是如何设计的。当我调用上面的命令时,变量%systemroot%
被扩展为C:\Windows
. 我尝试了以下不使变量扩展,但我无法强制它不扩展:
- 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:
- 用 `%, ^, \` 转义 `%%` - 即使我使用双引号也不起作用
- 在整个 /d 字符串周围使用单引号 ''
- 使用`setlocal setdelayedexpansion`?喜欢:
# (setlocal enabledelayedexpansion) && (reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d "!systemroot!\system32") && (setlocal disabledelayedexpansion)
# (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.
变量 'data' (/d) 字段是 like^%systemroot^%
或 like!systemroot!
或只是扩展到 C:\windows 。我可能可以使用 .reg 文件来完成我的任务,但我只是不想这样做。
我想可能是我用来显示变量内容的程序有问题(regedit / regedt32 / reg query (commandline)
),但经过检查后可能并非如此。
有任何想法吗?我主要感兴趣的是变量值在 regedit 窗口中应该是什么样子,它应该像 :"%systemroot%\system32"
还是"C:\windows\system32"
被其他程序正确扩展。问候。
回答by aphoria
From a command line, this worked for me:
从命令行,这对我有用:
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot^%\system32
回答by mivk
The syntax suggested by aphoria is correct, without quotes as you discovered. But you run into trouble when you need spaces in the value.
aphoria 建议的语法是正确的,没有您发现的引号。但是当您需要值中的空格时,您会遇到麻烦。
However, it ispossible 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
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")
(cmd.exe 总是让我想起 Henry Spencer 的名言“那些不懂 Unix 的人注定要重新发明它,很糟糕”)
回答by codelion
We have a use in an autounattend.xml. I was first following aphoria's answer, then I had to use mivk's enhanced answer. I had it working. Then I made a small change and it stopped working. I consider this insight worth reporting, to save others lengthy frustration.
我们在 autounattend.xml 中有一个用途。我首先关注 aphoria 的答案,然后我不得不使用 mivk 的增强答案。我让它工作。然后我做了一个小小的改变,它停止了工作。我认为这个见解值得报告,以免其他人长时间受挫。
Building on previous answers, here is how to make this work in autounattend.xml:
基于以前的答案,以下是如何在 autounattend.xml 中进行此工作:
<CommandLine>cmd.exe /c reg.exe add HKLM\ourkey /v ourvalue /t REG_EXPAND_SZ /d "our data with spaces"^%USERNAME^%"and more spaces" /f</CommandLine>
What matters is the cmd.exe /c. If you don't use cmd.exe then reg.exe still runs, but it puts literally ^% into the registry, not the desired %.
重要的是 cmd.exe /c。如果您不使用 cmd.exe,那么 reg.exe 仍会运行,但它实际上将 ^% 放入注册表中,而不是所需的 %。
Maybe the answer by ASTX813 would work, but this seems easier to read.
也许 ASTX813 的答案会起作用,但这似乎更容易阅读。
回答by ASTX813
I recognize that this is an old thread, but since there doesn't seem to be an answer and I too was driven crazy by this problem, I'll share what I've come up with. It's convoluted, it's ugly, and it looks like this:
我承认这是一个旧线程,但由于似乎没有答案,而且我也被这个问题逼疯了,我将分享我的想法。它是复杂的,它是丑陋的,它看起来像这样:
SET VAR=% SET SET VALUE=%VAR%SystemRoot%VAR%\system32... REG ADD HKLM... /v Test /t REG_EXPAND_SZ /d %VALUE%
SET VAR=% SET SET VALUE=%VAR%SystemRoot%VAR%\system32... REG ADD HKLM... /v 测试 /t REG_EXPAND_SZ /d %VALUE%
I end up with Test containing the string %SystemRoot%\system32
我最终得到包含字符串的 Test %SystemRoot%\system32
Edit: Should have finished testing before posting. While this worked from the command line, it failed in my command scripts. What DID work was SET VALUE=%%SystemRoot%%\system32...
编辑:应该在发布前完成测试。虽然这在命令行中起作用,但它在我的命令脚本中失败了。DID 的工作是什么SET VALUE=%%SystemRoot%%\system32...
回答by user4856514
There's no magic here, you must escape every % in command-line with/become: %%.
这里没有魔法,你必须用/变成:%% 来转义命令行中的每个 %。
In batch file, you again must escape them, become %%%%SystemRoot%%%%. Yes, four ampersands. It's ugly, but it's the way it is, and it's pretty consistent though.
在批处理文件中,您必须再次对它们进行转义,成为 %%%%SystemRoot%%%%。是的,四个&符号。这很丑陋,但它就是这样,尽管它非常一致。
note: Using ^ to escape it as literal character might help/cleanup in one step/degree only (as long as it's not interpreted). The advantage using ^ is that you can write in command-line or batch file with identical syntax: eg. "^%SytemRoot^%", since in both environments, % treated equally as literal.
注意:使用 ^ 将其转义为文字字符可能仅在一步/程度内帮助/清理(只要它不被解释)。使用 ^ 的优点是您可以使用相同的语法在命令行或批处理文件中编写:例如。“^%SytemRoot^%”,因为在两种环境中,% 都被视为文字。
回答by jaylweb
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.
这花了我一段时间才弄明白。在我看来,我应该引用整个字符串,但无法在不扩展它的情况下传递变量。我通过引用空格来打破我的思维方式。
回答by Robesini
Please try the following:
请尝试以下操作:
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot%^\system32
reg add HKCU\testfolder /t REG_EXPAND_SZ /v Stokrotka /d ^%systemroot%^\system32
回答by Fowl
From a batch file you end up with problems with quotes, which you can then escape with a backslash, for example:
从批处理文件中,您最终会遇到引号问题,然后您可以使用反斜杠进行转义,例如:
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%name%" /v "UninstallString" /t REG_EXPAND_SZ /d "\"%%comspec%%\" /d /c \"%dest%\uninstall.cmd\""
回答by pokrak
without quotes
没有引号
reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "%%%^%systemroot%%%^%\system32"
with quotes
带引号
reg add HKCU\testfolder /v Biedronka /t REG_EXPAND_SZ /d "\"^%%systemroot^%%\system32\""