Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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"

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.

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.

like image 811
Trey Avatar asked Mar 05 '12 01:03

Trey


4 Answers

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
like image 109
Marc Avatar answered Oct 20 '22 22:10

Marc


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

like image 29
dbenham Avatar answered Oct 20 '22 21:10

dbenham


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..... )

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!

like image 3
selnomeria Avatar answered Oct 20 '22 23:10

selnomeria


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
like image 1
AllGamer Avatar answered Oct 20 '22 21:10

AllGamer