REG ADD Ignores /f

3

My script:

@ECHO OFF

SET RegPath="HKEY_CURRENT_USER\Software\Microsoft\VBA\7.0\"

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v LastKey /t REG_SZ /d "%RegPath%" /f

START RegEdit

Despite the inclusion of /f, I am always prompted to replace the entry. How can I get the REG ADD command to not prompt and just replace the entry?

Kevin Jones

Posted 2017-04-11T19:16:04.013

Reputation: 33

Try to use reg import. – harrymc – 2017-04-11T19:56:35.830

Answers

5

If you run it as is, you'll see that the value added to the registry is actually HKEY_CURRENT_USER\Software\Microsoft\VBA\7.0" /f -- including an orphaned quotation mark, and the /f that was intended as a separate argument.

The problem here is that you're adding too many quotes via the variable itself, not to mention in the call to the variable, and as such it's confusing Reg about where arguments begin and end.

Remove all those extraneous quotation marks, and it works as intended:

@ECHO OFF

SET RegPath=HKEY_CURRENT_USER\Software\Microsoft\VBA\7.0\

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v LastKey /t REG_SZ /d %RegPath% /f

START RegEdit

Ƭᴇcʜιᴇ007

Posted 2017-04-11T19:16:04.013

Reputation: 103 763

Thanks. Quotes in batch/bash scripts are truly the one thing I hate the most in computing. – airstrike – 2017-04-11T20:16:55.420

1Argh! That was sort of the issue. Actually, it was two separate issues. One was the trailing backslash in the second line is interpreted as an escape character. I do need one set of quotes in the event there are spaces in the /d parameter. This is what I ended up with that works: SET RegPath=blah REG ADD ... /d "%RegPath%" /f – Kevin Jones – 2017-04-12T20:19:50.050