0

I have a really bad script ( I know there are much easier ways to achieve this, but oh well.) that calculates your ip address. It uses a very complicated and slow round-about method, but I am stuck. I basically have this: A colossal txt file on my desktop with an IP address embedded into it.

Here's the code:

@if (@CodeSection == @Batch) @then

@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

cd\
cd users\%username%\desktop\
if exist "what is my ip address - Google Search.htm" del "what is my ip address -     Google Search.htm"
if exist ip.txt del ip.txt

:: this link is just a shortened url to a google search of "what is my ip address"
start http://goo.gl/L66owU
timeout 10 >nul /nobreak

%sendkeys% "(^s)"
timeout 1 >nul /nobreak
%sendkeys% "{enter}"
timeout 5 >nul /nobreak

rename "what is my ip address - Google Search.htm" "ip.txt"
findstr /c "Your public IP address is <em>.............</em>"  ip.txt
pause
goto :EOF


@end


// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

I cringe when looking at it.

Anyway, my bug is in this line findstr /c "Your public IP address is <em>.............</em>" ip.txt I need to filter out everything, and get whatever value is set as the wildcards (the .......s)

If you try out the script, you will basically have your default browser open up a google page, and then it will save that page as an HTM file (if you use chrome at least), and I then rename the .html to .txt. I just need a better (and working) method of displaying the IP address in the end.

I know its possible, I'm just not very good with the find or findstr commands.

2 Answers2

0

Try this

for /f "tokens=6 delims=(" %%a in ('findstr /c:"Your public IP address is <em>"  ip.txt') do set ip="%%a"
set ip="%ip:*Your public IP address is <em>=%
set ip=%ip:~0,12%"

The problem is that Findstr returns the full line that contains the string specified so you would need a for loop to do the trick.
However, the for loop has a limitation of 31 tokens so I chose the "(" for the delimiter.

Remember: This script will only work as long the most recent save location was on the desktop.

Community
  • 1
  • 1
Ir Relevant
  • 963
  • 6
  • 12
0

There are various other option to do this very easily using VBS or other scripting langage. You can use cURL too. Like this :

@echo off 
for /f  "delims=" %%a in ('curl ifconfig.me/ip') do echo Your External IP adress is : %%a
pause

If you want to use your solution it will be better to use this Url to get a smaller html file:

http://checkip.dyndns.org/

so here your code modified with this URL :

@if (@CodeSection == @Batch) @then

@echo off

rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"

cd\
cd users\%username%\desktop\

:: this link is just a shortened url to a google search of "what is my ip address"
start http://checkip.dyndns.org/
timeout 10 >nul /nobreak

%sendkeys% "(^s)"
timeout 1 >nul /nobreak
%sendkeys% "{enter}"
timeout 5 >nul /nobreak
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type "Current IP Check.htm" ^| find /i "Address"') do set $line=!%%a%!
echo Your external IP is : %$line:~1%
goto :EOF


@end


// JScript section

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0))

;

SachaDee
  • 9,245
  • 3
  • 23
  • 33