REG ADD REG_SZ where Value contains embedded double quotes

0

This is driving me bananas but it must be something very simple. I am trying to add an ImagePath Value (REG_SZ) in a Batch script using REG ADD, where the Value Data contains embedded "double quotes". But I keep getting an "Invalid Syntax" error. This is the Value I am trying to add:

Key  : HKLM\SYSTEM\CurrentControlSet\Services\MSSQL$SQLEXPRESS
Value: ImagePath REG_SZ
Data : "C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Binn\sqlservr.exe" -sSQLEXPRESS 

I tried enclosing the Data in single quotes:

@SETLOCAL
@SET IPATH="C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Binn\sqlservr.exe" -sSQLEXPRESS
@REG.EXE ADD "HKLM\SYSTEM\CurrentControlSet\Services\MSSQL$SQLEXPRESS" /f /v ImagePath /t REG_SZ /d '%IPATH%'

I also tried enclosing in "double quotes", bang (!), [brackets], \backslashes\ and ^carets^ but I keep getting the syntax error.

Any idea what I am doing wrong?

Neil Weicher

Posted 2019-07-26T11:24:08.890

Reputation: 127

Answers

1

The parameter needs to be double-quoted. Other randomly chosen punctuation (single quotes, exclamation marks, ...) is not going to work.

However, you need to prefix the inner double-quotes with backslashes:

@SET ImagePath=\"C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\Binn\sqlservr.exe\" -sSQLEXPRESS
@REG.EXE ADD "HKLM\SYSTEM\CurrentControlSet\Services\MSSQL$SQLEXPRESS" /f /v ImagePath /t REG_SZ /d "%ImagePath%"

Note 1: To assign values in Cmd you need to use SET (as in SET VAR=VALUE).

Note 2: Do not use PATH as the variable name, as it is already used by Cmd for specifying program locations and overwriting it will make the script unable to run REG.EXE.

user1686

Posted 2019-07-26T11:24:08.890

Reputation: 283 655

thanks for pointing out the typo and PATH misuse - fixed. – Neil Weicher – 2019-07-26T11:35:29.767

your answer made me realize that I did not post the question properly. Rather than an extensive redo, I will create a new question. Should I delete this one? – Neil Weicher – 2019-07-26T12:00:08.533

Question has been replaced by this question

– Neil Weicher – 2019-07-26T12:48:56.287