Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing %~DP0 to Registry using REG ADD

I've resolved the question I had about inserting %DATE% into a REG_SZ registry value (see link), but now I'm running into a slightly different problem trying to insert %~DP0 (long source path) into a registry value using REG ADD within a .BAT script. It won't do it, and I'm sure it's because I'm doing something wrong.

reg add "hklm\software\acme" /v "TestValue" /d "%~dp0" /t REG_SZ /f

I've also tried setting the value to a variable first, but that doesn't work either. What happens is that it inserts the expanded path without the preceeding double-quote, but with a trailing double-quote, and then bombs with an error about REG /? syntax, etc.

SET VX=%~DP0
reg add "hklm\software\acme" /v "TestValue" /d "%VX%" /t REG_SZ /f

Anyone see what I'm doing wrong?

like image 729
Skatterbrainz Avatar asked Jan 12 '12 17:01

Skatterbrainz


1 Answers

The path %~dp0 ends in the directory separator character '\' (e.g. 'c:\temp\') which is being interpreted as an escape for the following double-quote character and so the parser is not seeing the closing double-quote. What you need to do is escape the trailing \ character with another one:

reg add "hklm\software\acme" /v "TestValue" /d "%~dp0\" /t REG_SZ /f
like image 170
zdan Avatar answered Nov 20 '22 08:11

zdan