How can I add a value to the registry via command line without a .REG file?

2

I have a need to add my external IP address to the Windows registry on a scheduled basis, and I want to do this with a bat/cmd file to automate the process.

I was able to use the advice from this post on this site to use curl.exe to get my external IP using http://icanhazip.com and set that IP as a variable within the batch process.

Now I just need a way to take that variable and inject it into the registry.

I know reg.exe or regedit.exe can add information to the registry by referencing a .REG file, but I don't quite know how to take the variable I get and add it without a .REG file.

One thing I already tried was using a .REG file already created, copying that to a new file (to preserve the original .REG file for reuse), then use echo to place the variable into the .REG file, like:

echo "some_reg_value"="%externalIP%" >> addIP.reg

The problem with this is the %externalIP% variable is adding an additional space after the IP address, so it looks like this in the .REG file and in the registry once the .REG file is added:

"some_reg_value"="192.168.1.100 " 

That extra space at the end causes a serious issue for the purpose I am trying to use it for.

Is there possibly a way to remove that extra space from the variable?

This is how I am getting the variable:

%CURL%\curl http://icanhazip.com > %CURL%\publicIP.txt
for /f "delims= " %%G in (%CURL%\publicIP.txt) do set PublicIP=%%G & del %CURL%\publicIP.txt

Any help would be greatly appreciated. It has been quite a while since I tried to do a whole lot with batch scripting and I can't remember a lot of the available commands.

T-Fed

Posted 2013-01-22T03:38:33.210

Reputation: 23

I found this post on superuser.com after I posted my question. I knew there was a way to use reg.exe to add a key without the need for a .REG file. Now I just need to see if using this with my variable still adds an extra space at the end. I have a feeling it will, so if anyone knows how to remove that I'd appreciate it!

– T-Fed – 2013-01-22T03:42:21.727

curl http://icanhazip.com > %CURL%\publicIP.txt for /f "delims= " %%G in (%CURL%\publicIP.txt) do set PublicIP=%%G& del %CURL%\publicIP.txtecho

– Ganesh R. – 2013-01-22T04:41:22.107

Thanks Ganesh R. I was able to get the "reg add" line to work, so I no longer need to add that variable to a .REG file. The "reg add" syntax is much cleaner IMO. – T-Fed – 2013-01-22T05:14:24.403

@T-Fed: I’m glad I could help you.  By the way, welcome to Super User.  For your information, when you respond to a comment (in a new comment), it’s conventional to mention the author’s name, preceded by “@”, as in “@GaneshR.” (don’t include spaces in the name).  That way he gets notified.  You can abbreviate, and you can mention multiple names, as in “@Ganesh, @Scott” (but there may be a limit of two or three names).  See the Replying in comments paragraphs of the Comment formatting section of the Markdown Editing Help page.

– Scott – 2013-01-22T18:03:08.170

Answers

3

I suspect that the solution is painfully “obvious”:

… do set PublicIP=%%G& del %CURL%\publicIP.txt

i.e., don’t include a space at the end of the set PublicIP= command.

However, another useful trick to know about is %PublicIP:~0,-1%, which is %PublicIP% with the last character removed.  See help set.

Scott

Posted 2013-01-22T03:38:33.210

Reputation: 17 653

Thanks for the speedy reply! I copied that syntax from that other post and wasn't sure of everything it was doing. Now that you point this out, I remember the "&" is used to string commands together on one line but executed in succession... I guess I didn't realize the space between would add a space. HOWEVER, I decided to try the "reg add" route without a .REG file and it does NOT add the extra space and everything is working for me now!! I'm very happy, and the batch file is now a lot shorter. ;-) Thanks again for the replies! – T-Fed – 2013-01-22T05:11:37.070

I’ve repeatedly been caught off-guard by the fact that echo Scott > myname.txt writes Scott (with a trailing space) to myname.txt.  To avoid that, I must say echo Scott> myname.txt, which I believe to be ugly –– but, hey, it’s Windows. – Scott – 2013-01-22T16:10:49.187