Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a registry key with default value containing double quotes and percent sign?

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:

Error: Invalid command-line parameters.

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?

like image 428
Victor Ponomarenko Avatar asked Dec 17 '14 23:12

Victor Ponomarenko


1 Answers

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

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.

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 /? 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.

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.

Example:

reg add HKCU\Environment /v "Please Delete" /t REG_SZ /d "Please delete this variable with a backslash \ inside and ending with 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.

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.

like image 137
Mofi Avatar answered Oct 02 '22 05:10

Mofi