Search code examples
windowsbatch-fileregistryadd-in

Add Registry Key Data With Quotes


I have written a batch that adds a new registry value under a specified key. The data value is a file path and must have outer quotes like so:

"C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam"

enter image description here

But even when using escape characters to keep the quotes, the closest I've been able to get is this (missing first quote):

C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam"

using this code:

setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS

SET VERSION=15.0
SET PATH="C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam"

REG add HKEY_CURRENT_USER\Software\Microsoft\Office\%VERSION%\Excel\Options /v OPEN /t REG_SZ /d %PATH%^" /f

If I try to add the carrot and quote to the beginning of the path, the batch doesn't add the value at all.

I've also tried using \ to keep the quotes to the same effect: one at the end keep the last quote, one at the beginning keeps the value from being added altogether.

What am I doing wrong here? According to the answers to this question, what I'm doing should work...


Solution

  • Quotes need to be escaped on declaration when setting the variable and you should also put \" around the %PATH%, like this:

    setlocal enableDelayedExpansion setlocal ENABLEEXTENSIONS

    SET VERSION=15.0 SET PATH=\"C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam\"

    REG add HKCU\Software\Microsoft\Office\%VERSION%\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f