Add Registry Key Data With Quotes - windows

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

Related

Traverse path variable with FOR in Windows

My faithful path traversal method no longer works - it sees spaces as delimiters. It's been a while since I needed to do batch programming.
When using the FOR loop, the only one that allows delimiters are the FOR /F option.
I don't want to create a temporary file containing the path, was hoping to do something like below:
C:\Users>for /f "delims=;" %i in %path% do echo %i
C:\Program was unexpected at this time.
C:\Users>for /f "delims=;" %i in (%path%) do echo %i
\Common was unexpected at this time.
C:\Users>for /f "delims=;" %i in 'foo;bar' do echo %i
'foo was unexpected at this time.
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%a in (^"!path:^;^=^
% Do NOT remove this line %
!^") do echo %%a
Output example:
C:\Program Files (x86)\AMD APP\bin\x86_64
C:\Program Files (x86)\AMD APP\bin\x86
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Windows Live\Shared
C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static
C:\Program Files\Bandizip\7z
A perfect solution must be able to differentiate between ; delimiters and quoted ; literals.
Here is a very robust solution that always works using JREPL.BAT - a regular expression find/replace utility
for /f "eol=: delims=" %%F in (
'jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path'
) do echo(%%F
If all you want to do is list each folder, one line per folder, then simply remove the FOR /F loop
call jrepl "([^;\q]+|\q.*?(\q|$))+" $0 /x /jmatch /s path
If you want a pure batch solution that always works, then you can use:
#echo off
setlocal DisableDelayedExpansion
set "var=%var:"=""%"
set "var=%var:^=^^%"
set "var=%var:&=^&%"
set "var=%var:|=^|%"
set "var=%var:<=^<%"
set "var=%var:>=^>%"
set "var=%var:;=^;^;%"
set var=%var:""="%
set "var=%var:"=""Q%"
set "var=%var:;;="S"S%"
set "var=%var:^;^;=;%"
set "var=%var:""="%"
setlocal EnableDelayedExpansion
set "var=!var:"Q=!"
for %%a in ("!var:"S"S=";"!") do (
if "!!"=="" endlocal
if %%a neq "" echo %%~a
)
The above is a slightly improved version of jeb's original solution
Use a standard for loop rather than for /F "delims=;" with some smart sub-string replacement:
for %I in ("%PATH:;=";"%") do #echo %~I
Supposing PATH contains C:\Program Files\someapp1\app1.exe;C:\Program Files\someapp2\app2.exe;C:\Program Files\someapp3\app3.exe, you will receive the following output:
C:\Program Files\someapp1\app1.exe
C:\Program Files\someapp2\app2.exe
C:\Program Files\someapp3\app3.exe
This works only in case none of the paths are surrounded by " and none of them contains ;.
In a batch script the code must be changed to this:
for %%I in ("%PATH:;=";"%") do #echo %%~I
Follow these Instructions from Mongodb .
cd C:\
md "\data\db"
After this step, you could once check in "C:\Program Files\MongoDB\Server\<Version Number>\bin\mongod.cfg"
there you could see, a default option of DBPath
# Where and how to store data.
storage:
dbPath: %MONGO_DATA_PATH%
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
Now add the %MONGO_DATA_PATH% variable in Environment variables of your system as key, where value is the file path ( where you have created the data/db folder)
now you could start mongod process without passing an argument of dbPath.

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.

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

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?
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

Resources