1
0 Comments

I am becoming extremely frustrated, but I suspect that the issue I am facing is relatively straightforward. Specifically, I am attempting to adjust the ImagePath Value (REG_SZ) for a Service within a Batch script using the REG ADD command. The Value Data includes “double quotes” within it, which seems to be causing the problem as I am repeatedly receiving an error message stating “Invalid Syntax”.

The Value I am attempting to insert is as follows:

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 attempted to enclose %IPATHNEW% within various types of quotation marks, brackets, and backslashes, but despite my efforts, I am still encountering a syntax error. Do you have any suggestions as to what could be causing this issue?

Askify Moderator Edited question April 27, 2023