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)

  • Do you have to use a Cmd batch script specifically, rather than PowerShell or VBScript? – user1686 Jul 26 '19 at 13:07
  • PowerShell should be fine, although I am much less familiar with PowerShell than CMD script. Thanks. – Neil Weicher Jul 26 '19 at 13:23
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.

  • Bingo! That did the trick - thanks. – Neil Weicher Jul 26 '19 at 15:29
  • 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 Jul 26 '19 at 16:05
  • This might need a new question, fully documented. Drop me a note if you want me to have a look. – harrymc Jul 26 '19 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.