本文介绍了如何添加默认值包含双引号和百分号的注册表项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法成功运行包含此内容的批处理文件:

I can't run successfully a batch file with this content:

REG ADD HKEY_CLASSES_ROOT\hlpfile\shell\compress\command /d "compact.exe /C \"%1\"" /f

REG ADD HKEY_CLASSES_ROOT\hlpfile\shell\uncompress\command /d "compact.exe /U \"%1\"" /f

它导致错误消息的输出:

It results in output of the error message:

错误:无效的命令行参数.

我想在 Windows XP SP2 上创建上下文菜单元素并指定操作:

I want to create context menu elements and specify actions on Windows XP SP2:

[HKEY_CLASSES_ROOT\hlpfile\shell\compress]

[HKEY_CLASSES_ROOT\hlpfile\shell\compress\command]
@="compact.exe /C \"%1\""

[HKEY_CLASSES_ROOT\hlpfile\shell\uncompress]

[HKEY_CLASSES_ROOT\hlpfile\shell\uncompress\command]
@="compact.exe /U \"%1\""

批处理文件中的两个命令行有什么问题?

What is wrong with the two command lines in batch file?

推荐答案

使用以下内容覆盖每个注册表项的默认值或创建每个注册表项并添加默认值strong> 命令行中的值:

Use following to overwrite the default value of each registry key or to create each registry key and add the default value from command line:

REG ADD HKEY_CLASSES_ROOT\hlpfile\shell\compress\command /ve /d "\"C:\Full Path\compact.exe\" /C \"%1\"" /f
REG ADD HKEY_CLASSES_ROOT\hlpfile\shell\uncompress\command /ve /d "\"C:\Full Path\compact.exe\" /U \"%1\"" /f

在批处理文件中执行相同操作需要:

Doing the same from within a batch file requires:

@echo off
%SystemRoot%\System32\reg.exe ADD HKEY_CLASSES_ROOT\hlpfile\shell\compress\command /ve /d "\"C:\Full Path\compact.exe\" /C \"%%1\"" /f >nul
%SystemRoot%\System32\reg.exe ADD HKEY_CLASSES_ROOT\hlpfile\shell\uncompress\command /ve /d "\"C:\Full Path\compact.exe\" /U \"%%1\"" /f >nul

要添加注册表值而不仅仅是注册表项,总是需要指定 /ve 作为键的默认值或 /v "Name of Value" 加上注册表值的类型,当然还有分配给注册表值的值.

To add a registry value and not only a registry key it is always necessary to specify either /ve for the default value of the key or /v "Name of Value" plus the type of the registry value and of course the value to assign to the registry value.

在批处理文件中,百分号 % 必须使用额外的百分号进行转义,以便 Windows 命令处理器在执行命令、应用程序或脚本之前解析命令行将其解释为文字字符.原因是 % 在批处理文件中具有特殊含义,因为它可以在命令提示符窗口中运行时看到:

In a batch file a percent sign % must be escaped with an additional percent sign for being interpreted as literal character by Windows command processor parsing the command line before executing the command, application or script. The reason is that % has a special meaning in batch files as it can be seen on running in a command prompt window:

  • call/? 输出命令 CALL 的帮助,解释如何使用百分号和参数编号来引用批处理文件参数,不带或带一个或多个修饰符或一个百分号和一个星号来引用除参数 0 之外的所有参数;
  • for/? 输出命令 FOR 的帮助,解释了如何在 Windows 命令行中使用百分号或批处理文件中的两个百分号引用循环变量以及没有或有一个或多个修饰符的循环变量字符;
  • set/? 输出命令 SET 的帮助,解释如何引用环境变量,方法是在变量名的每一侧加上一个百分号,以便在解析时立即展开命令行或整个命令块,或者在每一侧都有一个感叹号,用于延迟扩展(如果启用了延迟环境变量扩展).
  • call /? which outputs the help for command CALL explaining how to reference batch file arguments by using percent sign and an argument number without or with one or more modifiers or a percent sign and an asterisk to reference all arguments except argument 0;
  • for /? which outputs the help for command FOR explaining how to reference a loop variable with one percent sign on Windows command line or two percent signs in a batch file and the loop variable character without or with one or more modifiers;
  • set /? which outputs the help for command SET explaining how to reference environment variables by enclosing the variable name withing one percent sign on each side for immediate expansion on parsing command line or entire command block or with one exclamation mark on each side for delayed expansion if delayed environment variable expansion is enabled at all.

请注意,命令 REG 解析的参数与大多数其他控制台应用程序或 cmd.exe 的内部命令不同.如果双引号后面有反斜杠,则双引号 " 不会被解释为参数字符串的结尾.在这种情况下,双引号被解释为文字字符,而反斜杠则被解释为转义字符对于双引号.需要在字符串值的末尾转义反斜杠,再加一个反斜杠以正确添加数据字符串.

Please note that command REG parses the arguments different to most other console applications or internal commands of cmd.exe. A double quote " is not interpreted as end of argument string if there is a backslash left to the double quote. In this case the double quote is interpreted as literal character and the backslash left to it as escape character for the double quote. It is necessary to escape a backslash at end of a string value with one more backslash to add the data string correct.

示例:

reg add HKCU\Environment /v "Please Delete" /t REG_SZ /d "Please delete this variable with a backslash \ inside and ending with a backslash\\"

该命令将环境变量Please Delete与字符串值Please delete this variable with a backslash \ inside and end to a backslash\ 添加到当前用户环境的持久列表中变量.数据值字符串中的反斜杠不得转义.

This command adds the environment variable Please Delete with string value Please delete this variable with a backslash \ inside and ending with a backslash\ to persistent list of current user environment variables. A backslash inside the data value string must not be escaped.

请注意,环境变量应使用命令 SETX 添加到 Windows 注册表中,而不是使用命令 REG 添加到上面的示例中,如果 %SystemRoot%\System32\setx.exe 存在且环境变量的值超过 1024 个字符.

Please note that environment variables should be added with command SETX to Windows registry and not with command REG as done by the example above if %SystemRoot%\System32\setx.exe exists and the value of the environment variable is not longer than 1024 characters.

这篇关于如何添加默认值包含双引号和百分号的注册表项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:52