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.
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.727curl 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.107Thanks 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