Overwriting a reg key in command line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goudeuk
Posts: 5
Joined: 18 Dec 2015 06:36

Overwriting a reg key in command line

#1 Post by goudeuk » 18 Dec 2015 08:43

Hello everyone

I am trying to change the following registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\BBWin\hostname
Type: REG_SZ
Data Value: %computername%

The default value is %computername% but I need to change it to the full computer name, like this for example:
PCname.domain.co.uk

How can I do this at the command prompt, please?

I tried to do it by getting the computer name into a variable and then pipe the "reg add" command . The output is: The operation completed successfully but it doesn't change the reg key.

Code: Select all

set name=%hostname% | reg add "HKCU\Software\BBWin\hostname" /f /v "%name%.domain.co.uk" /t REG_SZ /d "Yes"


Can someone help me please?

Many thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Overwriting a reg key in command line

#2 Post by foxidrive » 18 Dec 2015 10:03

Does it work if you put the set statement on the line above?

The pipe in that spot is not how a pipe is intended to be used.

goudeuk
Posts: 5
Joined: 18 Dec 2015 06:36

Re: Overwriting a reg key in command line

#3 Post by goudeuk » 18 Dec 2015 10:50

foxidrive wrote:Does it work if you put the set statement on the line above?


I don't understand. The set statement is in the one-liner.

foxidrive wrote:The pipe in that spot is not how a pipe is intended to be used.


I would appreciate it, if you could tell me how a pipe is intended to be used.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Overwriting a reg key in command line

#4 Post by Squashman » 18 Dec 2015 11:08

Why wouldn't you just execute the command like this. The variable will expand to whatever is assigned to that variable before it executes the REG command.

Code: Select all

reg add "HKCU\Software\BBWin\hostname" /f /v "%hostname%.domain.co.uk" /t REG_SZ /d "Yes"

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Overwriting a reg key in command line

#5 Post by penpen » 19 Dec 2015 06:40

goudeuk wrote:I would appreciate it, if you could tell me how a pipe is intended to be used.
If you are using pipes, you should notice that you are using additional command instances (executed within the actual commanmd instance):

Code: Select all

cmdInstance2 | cmdInstance3
The standard input stream (stdin) of the actual command instance is redirected to the stdin cmdInstance2.
The standard output stream (stdout) of command instance 2 is redirected to stdin of command instance 3.
The stdout of command instance 2 is redirected to stdou of the actual command instance.
The standard error stream (stderr) of command isntances 2 and 3 are redirected to stder of the actual instance.

The environment set of command instances 2 and 3 are copies of the one of the actual instance.

goudeuk wrote:I don't understand. The set statement is in the one-liner.
So when you change a variable in command instance 2, this variable will not be changed in command instance 3.

penpen

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Overwriting a reg key in command line

#6 Post by Aacini » 19 Dec 2015 11:54

goudeuk wrote:

Code: Select all

set name=%hostname% | reg add "HKCU\Software\BBWin\hostname" /f /v "%name%.domain.co.uk" /t REG_SZ /d "Yes"

. . .
I would appreciate it, if you could tell me how a pipe is intended to be used.


A pipe is used to feed a data that is usually read via the keyboard. For example, this test.bat Batch file:

Code: Select all

@echo off
set /P "var="
echo Value read: %var%

... read the value of "var" variable from the keyboard, so you may give such value via a pipe this way:

Code: Select all

echo %hostname% | test.bat

The REG command does NOT read a data via the keyboard, so the pipe is not useful in this case.

Even if REG command would read a data via the keyboard, the command placed at left side of the pipe should be ECHO, because SET command does NOT send any output to the screen.

-------------------------------------

The way to execute two commands in the same line is separating they with an ampersand, like this:

Code: Select all

echo Hello & echo World!

However, if the first command is a SET and you want to use the just assigned value in the second command, then there are problems related to delayed expansion. There are two ways to achieve this at the command-prompt; using delayed expansion:

Code: Select all

cmd /V:ON /C set "name=%hostname%" ^& reg add "HKCU\Software\BBWin\hostname" /f /v "!name!.domain.co.uk" /t REG_SZ /d "Yes"

... or using the CALL trick:

Code: Select all

set "name=%hostname%" & CALL reg add "HKCU\Software\BBWin\hostname" /f /v "%name%.domain.co.uk" /t REG_SZ /d "Yes"

For further explanations of this point, search for "delayed expansion".

-------------------------------------

However, all this stuff is absolutely unnecessary! Answer this: if you want add the value of A to X, what do you do: "SET /A X=X+A" or "SET B=%A% & SET /A X=X+B"? Why define B variable with the value of A when the value of A can be directly used? Well, just use the same reasoning in your problem (as Squashman indicated in his reply):

Code: Select all

reg add "HKCU\Software\BBWin\hostname" /f /v "%hostname%.domain.co.uk" /t REG_SZ /d "Yes"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Overwriting a reg key in command line

#7 Post by foxidrive » 19 Dec 2015 18:34

goudeuk wrote:
foxidrive wrote:Does it work if you put the set statement on the line above?


I don't understand. The set statement is in the one-liner.

In the spirit of teaching a technique to someone who isn't too sure, when programming it is always useful, and even vital, to try different things to find out if it works, and also to verify that some code functions properly.

If you had simply tried what I suggested, you would have gained some knowledge and perhaps come back with a far better question.

goudeuk
Posts: 5
Joined: 18 Dec 2015 06:36

Re: Overwriting a reg key in command line

#8 Post by goudeuk » 21 Dec 2015 07:58

foxidrive wrote:
goudeuk wrote:
foxidrive wrote:Does it work if you put the set statement on the line above?


I don't understand. The set statement is in the one-liner.

In the spirit of teaching a technique to someone who isn't too sure, when programming it is always useful, and even vital, to try different things to find out if it works, and also to verify that some code functions properly.

If you had simply tried what I suggested, you would have gained some knowledge and perhaps come back with a far better question.



Your answer does not have any educational intention what's so ever. You said "that's not how a pipe should be used" and that was it. Don't you think you should elaborate a bit and explain how it should be used? I couldn't try what you suggested because I DIDN'T UNDERSTAND what you wanted me to try.

goudeuk
Posts: 5
Joined: 18 Dec 2015 06:36

Re: Overwriting a reg key in command line

#9 Post by goudeuk » 21 Dec 2015 08:04

Squashman wrote:Why wouldn't you just execute the command like this. The variable will expand to whatever is assigned to that variable before it executes the REG command.

Code: Select all

reg add "HKCU\Software\BBWin\hostname" /f /v "%hostname%.domain.co.uk" /t REG_SZ /d "Yes"


Squashman thank you for your reply

but the reg key stays unchanged although the return of your suggested command is: "The operation completed successfully".

goudeuk
Posts: 5
Joined: 18 Dec 2015 06:36

Re: Overwriting a reg key in command line

#10 Post by goudeuk » 21 Dec 2015 08:21

PenPen and Aacini

Thank you for your detail explanation and examples. Very helpful

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Overwriting a reg key in command line

#11 Post by foxidrive » 21 Dec 2015 09:20

goudeuk wrote:Your answer does not have any educational intention what's so ever. You said "that's not how a pipe should be used" and that was it. Don't you think you should elaborate a bit and explain how it should be used? I couldn't try what you suggested because I DIDN'T UNDERSTAND what you wanted me to try.


You saw my earlier reply and I realise now that I didn't understand your question...

...but I think my confusion was understandable - nobody types stuff like that at a command line.

Post Reply