String from file, how to set it as a Variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Saitto
Posts: 6
Joined: 23 Nov 2014 10:38

String from file, how to set it as a Variable?

#1 Post by Saitto » 28 Nov 2014 10:49

Greetings!
Anyone who can salve this ?
How to set specific word in string to a variabel and
later on echo it?

#Batch.bat
echo Ipconfigurations to file.
ipconfig > File.txt

#File.txt
Text .......

#Call Batch2.bat from Command prompt
type File.txt | findstr exactly "192"
:: set selected string to variabel.
:: Here is my problem! How can i set only 1
:: word from a specific string e.g. "192.168.1.20"
echo Your ip is: %ip%
192.168.1.20
pause

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

Re: String from file, how to set it as a Variable?

#2 Post by foxidrive » 28 Nov 2014 10:54

Do you want to find your LAN IP address?


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

Re: String from file, how to set it as a Variable?

#4 Post by Squashman » 28 Nov 2014 21:43

This is a neat trick that we recently learned about the PING command. If you don't specify an IP address to PING it just ping's your own IP address. You should still specify if you want IPv4 or IPv6 on Windows Vista and 7 machines.

Code: Select all

FOR /F "tokens=3 delims=: " %%G in ('ping -n 1 "" ^|find "Reply from" ') do set myip=%%G

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Re: String from file, how to set it as a Variable?

#5 Post by Xboxer » 29 Nov 2014 04:52

If you want to specify you can:

Code: Select all

for /f "tokens=2 delims=[]" %%f in ('ping -4 -n 1 www.DosTips.com ^|find /i "pinging"') do SET RemoteIp=%%f
echo Remote IP Address is: %RemoteIp%

Saitto
Posts: 6
Joined: 23 Nov 2014 10:38

Re: String from file, how to set it as a Variable?

#6 Post by Saitto » 01 Dec 2014 12:05

Sweet!
Yes as Local IP
External IP if you know how to also hehe

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

Re: String from file, how to set it as a Variable?

#7 Post by Squashman » 01 Dec 2014 12:30

Saitto wrote:External IP if you know how to also hehe

Are you referring to the IP address of your Router that connects to the Internet?

aGerman
Expert
Posts: 4710
Joined: 22 Jan 2010 18:01
Location: Germany

Re: String from file, how to set it as a Variable?

#8 Post by aGerman » 01 Dec 2014 15:23

http://myip.dnsomatic.com/ is a page that only contains your plain external IP address (without any html tags etc.). For that reason it loads very fast and it can be processed very easily using a hybrid script.

Code: Select all

@if (@X)==(@Y) @end /* valid line in Batch and JScript

:: Batch Part:
@echo off &setlocal
for /f %%i in ('cscript //nologo //e:jscript "%~fs0"') do echo %%i
pause

exit /b


:: JScript Part: */
try {
  var objXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  objXMLHTTP.open("GET", "http://myip.dnsomatic.com/", false);
  objXMLHTTP.send();
  if (objXMLHTTP.readyState == 4 && objXMLHTTP.status == 200) {
    WScript.StdOut.WriteLine(objXMLHTTP.responseText);
    WScript.Quit(0);
  }
  else {
    WScript.StdErr.WriteLine("Unable to finish request or page not found.");
    WScript.Quit(1);
  }
}
catch (e) {
  WScript.StdErr.WriteLine(e.name + ": " + e.message);
  WScript.Quit(1); 
}

Regards
aGerman

Xboxer
Posts: 25
Joined: 27 Nov 2014 13:58

Re: String from file, how to set it as a Variable?

#9 Post by Xboxer » 01 Dec 2014 16:57

foxidrive also did a script for external ip on here: viewtopic.php?p=18333

Code: Select all

@echo off
:: ### IP ISP ADDRESS:
>>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML
For /F "Tokens=7 Delims=:<" %%a In ('cscript /nologo "%temp%\ip.vbs"') Do Set IP=%%a
Echo     IP Address: %IP:~1%
pause>nul
Last edited by Xboxer on 02 Dec 2014 05:36, edited 1 time in total.

Saitto
Posts: 6
Joined: 23 Nov 2014 10:38

Re: String from file, how to set it as a Variable?

#10 Post by Saitto » 02 Dec 2014 03:15

<?PHP

$html = file_get_contents('http://www.tibia.com/community/?subtopic=characters&name=Antizo');
$getvocation = strpos($html, 'Vocation:');
$findend = strpos($html, '</tr>', $getvocation);
$findstring = substr($html, $getvocation, $findend-$getvocation);
$vocation = str_replace('Vocation:','', $findstring);
$vocation = strip_tags($vocation);

echo $findstring;

this one works also :)

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

Re: String from file, how to set it as a Variable?

#11 Post by foxidrive » 02 Dec 2014 04:41

Saitto wrote:<?PHP

$html = file_get_contents('http://www.tibia.com/community/?subtopic=characters&name=Antizo');
$getvocation = strpos($html, 'Vocation:');
$findend = strpos($html, '</tr>', $getvocation);
$findstring = substr($html, $getvocation, $findend-$getvocation);
$vocation = str_replace('Vocation:','', $findstring);
$vocation = strip_tags($vocation);

echo $findstring;

this one works also :)



How do you launch that with a batch file?

Saitto
Posts: 6
Joined: 23 Nov 2014 10:38

Re: String from file, how to set it as a Variable?

#12 Post by Saitto » 02 Dec 2014 08:44

Post above was Javascript..

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: String from file, how to set it as a Variable?

#13 Post by Compo » 02 Dec 2014 15:03

You could always use PowerShell

Code: Select all

@Echo Off & SetLocal
For /F %%A In ('PowerShell -C^
 "(Invoke-WebRequest http://ipinfo.io/ip).Content.Trim()"') Do Set PubIP=%%A
Echo( %PubIP%
>Nul Timeout 5

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

Re: String from file, how to set it as a Variable?

#14 Post by Squashman » 02 Dec 2014 20:13

Code: Select all

for /F "tokens=2 delims= " %%G in ('nslookup myip.opendns.com resolver1.opendns.com') do set externalip=%%G

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

Re: String from file, how to set it as a Variable?

#15 Post by Squashman » 02 Dec 2014 20:15

Saitto wrote:Post above was Javascript..

How do we execute that code?

Post Reply