Trouble with one reg add in a batch file - batch-file

I have a batch file with two reg add commands in it. Both seem to work but one will show in the registry for about 5 seconds and then disappears. I have tried all the formatting styles and cannot get the one to stick. Here are the two reg adds I'm using:
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v CrashPlanTray /t REG_SZ /d C:\Users\%USERNAME%\AppData\Local\Programs\CrashPlan\electron\CrashPlanDesktop.exe --menubar --desktop=false --user.install
and
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v CrashPlanServiceUser /t REG_SZ /d C:\Users\%USERNAME%\AppData\Local\Programs\CrashPlan\CrashPlanService.vbs
It is the first one CrashPlanTray that will not stick.
I have tried it adding the \ before the data value and at the end of the data value. It will add it to the registry but then after a few seconds it disappers.
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v CrashPlanTray /t REG_SZ /d "\"C:\Users\%USERNAME%\AppData\Local\Programs\CrashPlan\electron\CrashPlanDesktop.exe\" --menubar --desktop=false --user.install
I have been trouble shooting and working with this for days now. I can get it to work and stick from a command window but once I put it in the batch file it no longer sticks. I could use some expert help in figuring out why it won't stick in the registry?

On the first reg add, try this:
reg add HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run /v CrashPlanTray /t REG_SZ /d "\"%localappdata%\Programs\CrashPlan\electron\CrashPlanDesktop.exe\" --menubar --desktop=false --user.install"
Take special note of the double quotes (beginning and end) enclosing the full string to registry, as well as the escaped ones enclosing the path ( I shortened the above path for demonstration purpose:
"\"%localappdata%\..\CrashPlanDesktop.exe\" --menubar --desktop=false --user.install"
Finally, also notice we can use %localappdata% instead of C:\users\%username%\..

First thing I'd do is because the locations use environment variables is to use REG_EXPAND_SZ, instead of REG_SZ.
I'd use backslashes to then escape any internal doublequotes and protect the locations, as they could contain things like spaces.
I'd set the common strings as values at the top to make it easier to modify and shorten the lines somewhat, and would also probably use carets, ^ to shorten the lines still further for readability.
Finally, if you're wanting to run a VBScript from the registry run key, you should really be running it from either WScript.exe or CScript.exe. My example below uses WSCript, but you can replace that with CScript and any required options as needed.
#Set "Key=HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"
#Set "Loc=%%LocalAppData%%\Programs\CrashPlan"
#Reg Add "%Key%" /V "CrashPlanTray" /T REG_EXPAND_SZ /D^
"\"%Loc%\electron\CrashPlanDesktop.exe\" --menubar --desktop=false --user.install" /F>Nul
#Reg Add "%Key%" /V "CrashPlanServiceUser" /T REG_EXPAND_SZ /D^
"WScript \"%Loc%\CrashPlanService.vbs\"" /F>Nul

I don't know exactly what has changed but after completely uninstalling and reinstalling CrashPlan, all is working again. I can now run the batch files to disable and re-enable it they are now working correctly. Your efforts were not wasted as I am using snippets of the things you suggested in my revised batch files and it makes them much cleaner to read and work with. Thank you for your help and patience.

Related

NSIS with admin access fails to create reg key via batch script

I have a NSIS script with RequestExecutionLevel admin set and within this I invoke a .bat script which adds a reg key.
When the batch-file is executed through command-prompt the reg key gets added. But when running the installer, it executes the .bat file but fails to add the reg key.
nsExec::ExecToStack '"$pluginsdir${SETUP_PATH}\UpdateNtpConfiguration.bat" $Ip1'
UpdateNtpConfiguration.bat content
set adds=%1
REM Get the list of ntp servers showing up in System Date & Time->Internet Time dropdown
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1
REM Add address at the end
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "%adds%" /f
goto:eof
Just like #Anders said, there are built in functions for that.
https://nsis.sourceforge.io/Docs/Chapter4.html#registry
If an an error happens with this command, it will be a lot easier to debug as it is built-in.
If you're not doing anything else in the batch file, it is better to find out if NSIS has a command already integrated with it....
EDIT:
It also might be that the admin installer is run the bat file without admin privs.
After some debugging found out that NSIS was writing the registry values in 32 bit reg space(HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft). I had to append /reg:64 to make it add to the 64 bit reg space.
Also the reason I had the bat script was to have some logic to look for duplicates before adding the key.

Why does this bat file not execute properly?

I need to add a subkey in the registry of Windows 10 with a .bat script, but the results are not what is expected. Code does produce a result, but creates an incorrect entry. I need a subkey named EnableLinkedConnections that is of the type: REG_DWORD with a data value of 1
I have already written a .bat to add entries which are correct, but trying to add another in a different registry folder creates an incorrect entry.
#echo off
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\" /f /v "EnableLinkedConnections" /t REG_DWORD d/ 0x1
pause
This creates a folder outside of the System folder, but still in the Policies folder named:
System" /f /v EnableLinkedConnections /t REG_DWORD d/ 0x1
which is not correct. I expected a subkey entry of type REG_DWORD.
Seems that its having an issue with backslashes maybe, but the other .bat I wrote does not do this, so a bit stumped. Any pointers welcome.

Add registry from bat file

I am trying to add registry entry from bat file. I have a reg file that contains
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001
It works if you double click the reg file. However, I need to make the same registry edit from bat file. I have tried this
reg add HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel /v HomePage /t REG_DWORD /d 0 /f
But doesnt work. What is the correct way to do that ? (except running .reg file from bat file)
Put quotes around the key name.
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v HomePage /t REG_DWORD /d 0 /f
Because "Internet Explorer" and "Control Panel" contain spaces, you need quotes so it is all treated as one parameter.

Change regional and language options in BATCH

How can I change the regional and language options in Windows XP and 7 using batch code?
I want to change the "Standards and formats" to Mexican Spanish with a .bat file.
Those settings are in the registry under HKCU\Control Panel\International
You can use reg.exe to make the changes manually. The easiest way to do that is to manually change your region and language to spanish (mexico) open a cmd window and type reg query "HKCU\Control Panel\International" which will show you the values as you want them. Then to modify them, use REG ADD "HKCU\Control Panel\International" /t REG_SZ /v LocaleName /d es-Mx /f for each value replacing what is after /v with the appropriate name and what is after /d with the appropriate value.
The other option is to just export the HKCU\Control Panel\International hive to a .reg file and just import it into the registry using regedit /s ImportFile.reg
You may need to refresh the registry after the import to see the changes. This usually involves a reboot but try adding the following as the last line in your batch file instead. RUNDLL32.EXE USER32.DLL,UpdatePerUserSystemParameters ,1 ,True

Registry Duplicate Key

So I am writing one of my first large batch scripts and part of the script needs to create several keys in the registry. The problem I am having is that a redundant subordinate key is being created in the path and I am not sure how to solve it. Also of note is that this only occurs on certain systems and is not always the case.
Here is the command I am using:
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Riedel\ARTIST SNMP Agent" /v "MasterSnmpAgentIpAddr" /t REG_SZ /d "127.0.0.1:705"
When I look in the registry to see the result, here is the path that it is placed in:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Wow6432Node\Riedel\ARTIST SNMP Agent
I've tried a few methods of creating registry keys, but they yield the same results. Any thoughts or help would be appreciated. Thanks!
Wow6432Node is for the 32bit programs in 64bit OS. So you should better use the reg add switch /reg:
try this for 32bit entries
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Riedel\ARTIST SNMP Agent" /v "MasterSnmpAgentIpAddr" /t REG_SZ /d "127.0.0.1:705" /reg:32
and for 64bit
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Riedel\ARTIST SNMP Agent" /v "MasterSnmpAgentIpAddr" /t REG_SZ /d "127.0.0.1:705" /reg:64

Resources