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)
Answer
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.
No comments:
Post a Comment