REG ADD REG_SZ where Value contains embedded double quotes (redux)

1

This is driving me bananas but it must be something very simple. I am trying to modify an ImagePath Value (REG_SZ) for a Service 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\myservice
Value: ImagePath REG_SZ
Data : "c:\program files\mydir\old.exe" -helloworld

I am trying to change the ImagePath to:

Data: "c:\program files\mydir\new.exe" -helloworld

However I am getting a Syntax Error in the REG ADD command. This is the stripped down script:

@echo off
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
for /f "tokens=2*" %%A in ('REG.EXE QUERY "HKLM\SYSTEM\CurrentControlSet\Services\myservice" /v ImagePath') DO set IPATHOLD=%%B
SET IPATHOLD=%IPATHOLD:\0= %
echo OLDPATH^=%IPATHOLD%
set "OLDEXE=old.exe"
set "NEWEXE=new.exe"
for /f "delims=" %%A in ("%IPATHOLD%") do (
    set "string=%%A"
    set "IPATHNEW=!string:%OLDEXE%=%NEWEXE%!"
)
echo NEWPATH^=%IPATHNEW%
@echo on
@pause
if !IPATHNEW! NEQ !IPATHOLD! (
    @echo ready to change
    @REM next line results in Syntax Error
    REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\myservice" /v ImagePath /t REG_SZ /d "%IPATHNEW%" /f
)

I have tried enclosing %IPATHNEW% in "doublequotes", 'singlequotes', [brackets] and \backslashes\ but I keep getting the syntax error.

Any idea what I am doing wrong?

(Note - this is a replacement of this question: REG ADD REG_SZ where Value contains embedded double quotes)

Neil Weicher

Posted 2019-07-26T12:42:30.130

Reputation: 127

Do you have to use a Cmd batch script specifically, rather than PowerShell or VBScript? – user1686 – 2019-07-26T13:07:19.900

PowerShell should be fine, although I am much less familiar with PowerShell than CMD script. Thanks. – Neil Weicher – 2019-07-26T13:23:42.417

Answers

1

The command you are looking for is probably the one that will replace " by \":

    set "IPATHNEW=!IPATHNEW:"=^\"!"

The script might look like this:

setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
for /f "tokens=2*" %%A in ('REG.EXE QUERY "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\myservice" /v ImagePath') DO set IPATHOLD=%%B
set "IPATHNEW=!IPATHOLD:old=new!"
set "IPATHNEW=!IPATHNEW:"=^\"!"
echo "%IPATHNEW%"
@pause
if !IPATHNEW! NEQ !IPATHOLD! (
    REG ADD "HKLM\SYSTEM\CurrentControlSet\Services\myservice" /v ImagePath /t REG_SZ /d "%IPATHNEW%" /f
)

I tested it partially and it seemed to work.

harrymc

Posted 2019-07-26T12:42:30.130

Reputation: 306 093

Bingo! That did the trick - thanks. – Neil Weicher – 2019-07-26T15:29:42.023

One other question. How do I check !IPATHOLD! to see if it actually contains the string old before proceeding? I played around with various IF scenarios but kept getting "not expected now" errors. – Neil Weicher – 2019-07-26T16:05:15.477

This might need a new question, fully documented. Drop me a note if you want me to have a look. – harrymc – 2019-07-26T16:06:32.357