Why does this bat file not execute properly? - batch-file

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.

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.

REG ADD syntax issue

I'm trying to make a batch file that creates a entry in the registry.
The entry must be link to a executable. It needs two parameters :
(path) The folder to process (see below project1)
(path) The folder that will contain the file returned by the process. I want this to be the parent folder of the first parameter (see below DirectoryProject)
Tree :
../somewhere/my/program/program.exe
../DirectoryProject
../DirectoryProject/project1
../DirectoryProject/output.data
Exemple :
"../somewhere/my/program/program.exe" "../DirectoryProject/project1" "../DirectoryProject"
The first parameter is easy, it is %%1. It has the folder's path I right clicked as value. The last parameter is however harder to get.
I first tried to manipulate the variable %%1.
reg add "%RootKey%\Software\Classes\Directory\shell\%KeyName%\command" /VE /D "cmd /c cd \"%%1\" & \"%Exec%\" \"%%1\" \"%%~dp1\"" /F
The program logs the parameters it reveice. The last parameter is equal to %%~dp1 so it does not work.
Then I tried a tricky way using the first parameter and the CD command
reg add "%RootKey%\Software\Classes\Directory\shell\%KeyName%\command" /VE /D "cmd /c cd \"%%1\" & \"%Exec%\" \"%%1\" \"%CD%\"" /F
In this case %CD% will always be the path of the batch file so clearly not what I want nether
I'm still looking
Thanks for your interest and help
If the value of %1 is a fully qualified and existing path then the root folder would generally be the drive letter followed by a backslash, normally denoted by %~d1\. However, given your latest clarification, it appears your looking for the current working directory, commonly denoted as %CD%.
I'm assuming that you wanted to manipulate the second instance of %1, so based on my assessment, here's how I'd do it:
#Echo Off
Set "ExePath=C:\SomePathTo\MadeUp.exe"
Set "MenuTxt=Process with MadeUp"
Set "KeyName=LaunchTest"
Set "RootKey=HKCU"
Reg Add "%RootKey%\Software\Classes\Directory\shell\%KeyName%" /VE /D "%MenuTxt%" /F >Nul
Reg Add "%RootKey%\Software\Classes\Directory\shell\%KeyName%\command" /VE /D "\"%ExePath%\" \"%%~1\" \"%CD%\"" /F >Nul
Please make sure that you modify your specific executable path on line 2 as required.
Note that the text description you want in the context menu, currently Process with MadeUp can be changed to suit your preference, you can do that on line 3.
I have left your registry key name as you had it, LaunchTest, this can be changed in line 4, (when it's no longer a test).
A user menu entry should be entered into the HKCU key, so I'm using that.(If you want it for all users then change HKCU to HKLM on line 5. Whilst it does not match your provided key, it is the correct way to do it. Entries from both HKCU\Software\Classes and HKLM\SOFTWARE\Classes are auto propagated to HKCR, you shouldn't add things directly to it.If you change it to HKLM be aware that there's a likelihood the script will need to be run As administrator)
If you had wanted the root directory of the right clicked folder then you'd change \"%CD%\" to \"%%~d1\\\" on line 7.

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.

Reg command in batch file

I am trying to run a simple batch file with following line of codes in it but as soon as I trigger this batch file (either running it from command prompt or double clicking on it), it continuously runs. Like forever.
I am just trying to modify existing key in registry using batch file to some specific value.
reg add "hkcu\control panel\desktop" /v ForegroundLockTimeout /d "0x30d40" /f /t REG_DWORD
exit
In order to stop execution when I am giving interrupt (Cntrl + C) it asks whether it should terminate execution of this batch file or not..
Can someone help to understand what is wrong with this code?
I think it should stop execution once it updates registry key but that is not happening. It continuously prints line# 1 on screen.
You've called the batch file reg.bat right?
Try regfile.bat
Try the following, this works for me:
reg add "HKCU\control panel\desktop" /v ForegroundLockTimeout /t REG_DWORD /d "0x30d40" /f
It returns:
The Operation completed successfully.
Obviously try saving that to your bat file and then run the batfile as usual.

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