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"=" " 

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
    Jan 22, 2013 at 3:42
  • curl icanhazip.com > %CURL%\publicIP.txt for /f "delims= " %%G in (%CURL%\publicIP.txt) do set PublicIP=%%G& del %CURL%\publicIP.txtecho
    – Ganesh R.
    Jan 22, 2013 at 4:41
  • 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
    Jan 22, 2013 at 5:14
  • @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. Jan 22, 2013 at 18:03

1 Answer 1


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.

  • 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
    Jan 22, 2013 at 5:11
  • 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. Jan 22, 2013 at 16:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .