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

4

14

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.

Foliose answered 5/3, 2012 at 1:45 Comment(0)
N
23

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
Nidianidicolous answered 5/3, 2012 at 2:21 Comment(3)
You answered about halve my question. The output of what you gave me was "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "" " /f instead of "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*Foliose
Okay. I put the quotation before the /f so now it doesn't ask me if I want to override the existing registry key but the output is still relatively the same. "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "" The "%1" is turning just into "" and the %* isn't appearing.Foliose
Not sure why that's not working. As an alternative, you could export the registry key you want using the regedit.exe right-click menu, and then use "reg restore" in your batch file to restore the key. That would save you some of this trouble. Examining the exported file will also give you a clue to how to format for "reg add".Nidianidicolous
M
15

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

Marshall answered 5/3, 2012 at 4:35 Comment(2)
THANK YOU SO MUCH! Works 100% now.Foliose
Good call, dbenham. Running from command line vs. batch file explains the percentage issues.Nidianidicolous
S
3

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!

Smalt answered 15/10, 2013 at 8:56 Comment(0)
A
1

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
Altimetry answered 8/2, 2015 at 4:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.