Writing %~DP0 to Registry using REG ADD - windows

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?

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

Related

Batch, adding quotes to argument stops reg from working

So i tried to make a batch file that runs when a txt file is opened.
reg add HKCR\txtfile\shell\open\command /ve /t REG_EXPAND_SZ /d %0 /f
So far so good. But when i open the text document theres an error due to spaces in %0.
reg add HKCR\txtfile\shell\open\command /ve /t REG_EXPAND_SZ /d "%0" /f
This doesnt give me an error, but the registry value doesnt change.
reg add HKCR\txtfile\shell\open\command /ve /t REG_EXPAND_SZ /d ^"%0^" /f
Same thing.
So whats my problem here?
The escape character in this case, (complete lack of consistency, I know), is the back slash:
Reg Add "HKCR\txtfile\shell\open\command" /VE /D \"%~0\" /F >Nul

Adding a directory to the system path variable through registry

I am trying to add a directory to the PATH variable in windows. This is what I am entering the command line. (Or a batch file)
#echo off
set value=%path%;%ProgramFiles%\AtomScript\
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d %value% /f
And it comes up with this message
ERROR: Invalid syntax.
Type "REG ADD /?" for usage.
What am I doing wrong?
You probably have to quote %value% (with double-quotes) because its expansion has embedded blanks for C:\Program Files, etc.
That would be
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Sessions Manager\Environment" /v Path /t REG_EXPAND_SZ /d "%value%" /f
You can see what the actual expansions are by turning echo on in your script:
#echo on
Maybe you solved it already, but as far as I can see you also might have a misspelling in "...\Sessions Manager...". At least on my system it's "Session" without the extra s.

Add Registry Key Data With Quotes

I have written a batch that adds a new registry value under a specified key. The data value is a file path and must have outer quotes like so:
"C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam"
But even when using escape characters to keep the quotes, the closest I've been able to get is this (missing first quote):
C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam"
using this code:
setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS
SET VERSION=15.0
SET PATH="C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam"
REG add HKEY_CURRENT_USER\Software\Microsoft\Office\%VERSION%\Excel\Options /v OPEN /t REG_SZ /d %PATH%^" /f
If I try to add the carrot and quote to the beginning of the path, the batch doesn't add the value at all.
I've also tried using \ to keep the quotes to the same effect: one at the end keep the last quote, one at the beginning keeps the value from being added altogether.
What am I doing wrong here? According to the answers to this question, what I'm doing should work...
Quotes need to be escaped on declaration when setting the variable and you should also put \" around the %PATH%, like this:
setlocal enableDelayedExpansion
setlocal ENABLEEXTENSIONS
SET VERSION=15.0
SET PATH=\"C:\Program Files\Microsoft Office\Office15\Library\Custom_AddIn.xlam\"
REG add HKCU\Software\Microsoft\Office\%VERSION%\Excel\Options /v OPEN /t REG_SZ /d "%PATH%" /f

How to get the full path of a file properly?

I've to add a script to windows registry via command line as follow:
Reg add HKCU\Software\...\.\.. /v backUp /t REG_SZ /d "%~dp0\backUp.bat" /f
The script is called "setup.bat" it contains only the above line. BTW backUp.bat is running at the same dir as the "setup.bat" so for that i'm using %~dp0 to get the full path of the script dir. I surrounded %~dp0 by ", because some path contains spaces.. So, normally when i run i set properly the full path of the backUp.bat, whenever the script is running, but the problem is that i get a double slash before backUp.bat in my registry, look:
C:\Users\marwen\Desktop\bin\\backUp.bat
If i change the command as this:
Reg add HKCU\Software\...\.\.. /v backUp /t REG_SZ /d "%~dp0"\backUp.bat /f
Same here, the result is faulty :
C:\Users\marwen\Desktop\bin"\backUp.bat
How to solve this ?
untested but Reg add HKCU\Software\...\.\.. /v backUp /t REG_SZ /d "%~dp0backUp.bat" /f without the slash should do it.

Adding a registry key in windows with quotes needed in the data using a batch script

Little Willis here. I am trying to using a batch script to edit an existing registry key that is used when double clicking a .jar file. The issue is that the data that I'm trying to enter contains quotes but I also need quotes for it to be considered a string.
Example:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* /f
When I run that in a batch script the cmd window prints out "Error: Too many command line parameters"
So to make this simple. I want to add a registry key with "C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %* as the data including the quotations and the %1 and %* exactly as they are not converted to any actual statement or string.
EDIT:
The registry is normally added using using this command line string:
ftype jarfile="C:\Program Files\Java\jre7\bin\javaw.exe" -jar "%1" %*
it works fine in the command line, but just as the code given below when I used this in a batch script the "%1" and %* don't appear.
Use backslashes to escape the inner quotes, i.e.:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "\"C:\Program Files\Java\jre7\bin\javaw.exe\" -jar \"%1\" %*" /f
Percent literals must be doubled in a batch file: \"%%1\" %%*"
as an addition to dbenham's answer, you should use backslaches and quotes for location path !! (i mean, you should use "\"C:\Program Files..... instead of "C:\Program Files..... )
so this is the final answer for typical percent sign & adding problem:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "\"C:\Program Files\Java\jre7\bin\javaw.exe\" -jar \"%%1\"" /f
thanks dbenham!
Another alternative is to use single quotes, some applications can read it properly, example:
reg add "HKEY_LOCAL_MACHINE\Software\Classes\jarfile\shell\open\command" /v "" /t REG_EXPAND_SZ /d "'C:\Program Files\Java\jre7\bin\javaw.exe\' -jar '%1' %*" /f

Resources