使用批处理脚本在数据中使用引号在 Windows 中添加注册表项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9560709/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:18:24  来源:igfitidea点击:

Adding a registry key in windows with quotes needed in the data using a batch script

windowsbatch-filedouble-quotesregistrykey

提问by Trey

Little Willis here. I am trying to using a batch script to edit an existing registry key that is used when double clicking a .jar file. The issue is that the data that I'm trying to enter contains quotes but I also need quotes for it to be considered a string.

小威利斯来了 我正在尝试使用批处理脚本来编辑双击 .jar 文件时使用的现有注册表项。问题是我尝试输入的数据包含引号,但我也需要引号才能将其视为字符串。

Example:

例子:

reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* /f

When I run that in a batch script the cmd window prints out "Error: Too many command line parameters"

当我在批处理脚本中运行它时,cmd 窗口会打印出“错误:命令行参数太多”

So to make this simple. I want to add a registry key with "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* as the data including the quotations and the %1 and %* exactly as they are not converted to any actual statement or string.

所以为了让这一切变得简单。我想添加一个带有 "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* 的注册表项作为包括引号和 %1 和 %* 的数据未转换为任何实际语句或字符串。

EDIT:

编辑:

The registry is normally added using using this command line string:

通常使用以下命令行字符串添加注册表:

ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*  

it works fine in the command line, but just as the code given below when I used this in a batch script the "%1" and %* don't appear.

它在命令行中工作正常,但正如下面给出的代码一样,当我在批处理脚本中使用它时,“%1”和 %* 不会出现。

回答by Marc

Use backslashes to escape the inner quotes, i.e.:

使用反斜杠来转义内部引号,即:

reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "\"C:\Program Files\Java\jre7\bin\javaw.exe\" -jar \"%1\" %*" /f

回答by dbenham

Percent literals must be doubled in a batch file: \"%%1\" %%*"

百分比文字必须在批处理文件中加倍: \"%%1\" %%*"

回答by selnomeria

as an addition to dbenham's answer, you should use backslaches and quotes for location path !!
(i mean, you should use "\"C:\Program Files.....instead of "C:\Program Files.....)

作为dbenham答案的补充,您应该对位置路径使用 backslaches 和引号!!
(我的意思是,你应该使用"\"C:\Program Files.....而不是"C:\Program Files.....

so this is the final answer for typical percent sign &adding problem:

所以这是典型百分号&添加问题的最终答案:

reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "\"C:\Program Files\Java\jre7\bin\javaw.exe\" -jar \"%%1\"" /f

thanks dbenham!

谢谢德本汉姆!

回答by AllGamer

Another alternative is to use single quotes, some applications can read it properly, example:

另一种选择是使用单引号,一些应用程序可以正确读取它,例如:

reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "'C:\Program Files\Java\jre7\bin\javaw.exe\' -jar '%1' %*" /f