Extracting data from text file

January 11, 2010 at 07:35:23
Specs: Windows XP
Ok Basically i need to either extract part of a string from a text file or extract part of the string directly from the output of a different command.

Either i will have a text file with an ip address (eg Of which i would like to end up with "172.1.1." as a variable.

Or i would like to be able to resolve a hostname to an ip, (eg then remove the "2" and so therefore end up with 172.1.1. as a variable.

Any ideas?

thanks in advance

See More: Extracting data from text file

Report •

January 11, 2010 at 12:47:23
if you could be more specific about text file layout, and arrangement (ie: if multiple files - what naming pattern used)
and the source of the hostname (cmdline? file? prompted?)
it's hard to be of much help without more knowledge of these.

this can get the ip of a hostname (no slashes)
@echo off & setlocal
:: sample hostname, no slashes
set test=HP307W
:: next, up to "do (" is all one line!
for /f "tokens=2-5 delims=[]." %%a in ('ping -a -n 1 %test% ^| find "Pinging %test% ["') do (
echo.%%a %%b %%c %%d
set ip3=%%a.%%b.%%c
echo IP3:[%ip3%]

Report •

January 12, 2010 at 09:10:51
ok, just checked back here and i have actually solved this problem already, but come up against another.
Problem was solved by:
For /F "Skip=5 Tokens=*" %%I in ('ping %serv% -n 1') do echo %%I>>host.txt
set /P ip=<host.txt
del host.txt
set ip2=%ip:~0,-1%
set ip3=%ip2:~20,16%
set ip4=%ip3:~0,-3%
echo %ip4%

%ip4% is therefore equal to "172.168.0." for example

But my next problem comes, from a loop and trying to define a variable.
For /L %%x In (1,1,255) Do (
ping -n 1 %ip4%%%x | find /i "bytes=" > nul
if ErrorLevel 1 (echo %ip4%%%x is off-line) else (
For /F "Skip=1 Tokens=*" %%K in ('ping -a %ip4%%%x -n 1') do echo %%K>>ip.txt
set /P host=<ip.txt
Echo !host!
del ip.txt
set host2=!host:~0,7!
set host3=!host2:~15,26!
Echo !host3!

This will run fine untill
set host2=!host:~0,7!
at which point i dont seem to be able to set the variable host2.

Any ideas anyone?

Report •

January 12, 2010 at 11:45:34
i can't see anything wrong except on my system the first 7 bytes of the output from line2 from "ping" is "Pinging" and then host3
is trying to take nonexistant bytes from host2.
set host2=!host:~0,7! only 7 bytes long
set host3=!host2:~15,26! starts at nonexistant byte# 15. I think you wanted:
set host2=!host:~8!
but the "tokens" could have taken care of all that if setup to do so.

on a side note, why does my "ping" that came with windows xp not show the resolved hostname?it takes the -a just fine and proceeds to politely ignore it. i copied my ping over from the winME and that one works... go figure, MS strikes again.

Report •

Related Solutions

January 12, 2010 at 22:48:03
Here's a more striaghtforward way to drop the last octet.

@echo off > newfile & setLocal enableDELAYedeXpansion

for /f "tokens=* delims= " %%a in (hostlist) do (
for /f "tokens=3 delims= " %%i in ('ping -n 1 %%a^|find "Reply"') do (
for /f "tokens=1-3 delims=." %%x in ("%%i") do (
echo %%x.%%y.%%z

Helping others achieve escape felicity


Report •

Ask Question