Store whole out put into a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Store whole out put into a string

#1 Post by Adrianvdh » 08 Aug 2013 13:11

Hello everyone...

I am writing a huge program and it has the function to ping a website, with trouble handling. So it will ping with the command >nul, using user input website and if it responds positive it will show the output, but then my code has to ping the website again to show the output, is there anyway to store the 1st ping output in some sort of variable, then previous code I had was to save the ping output into a text file and use the type command to display that text file, but it is not the best way. Any help would be great.

And of course, thank you for your time :).

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Store whole out put into a string

#2 Post by penpen » 08 Aug 2013 13:28

This may work, as the filename "result.txt" is not in use:

Code: Select all

set "result=result.txt"
ping any.web.adress > "%result%"
if %errorlevel% == 0 type "%result%" & del "%result%"

penpen

Magialisk
Posts: 104
Joined: 25 Jul 2013 19:00

Re: Store whole out put into a string

#3 Post by Magialisk » 08 Aug 2013 20:39

Did you edit your post by chance after penpen posted his solution? It seems like his approach is exactly what you said you already tried with a temp file..?

In any case, here's what I would recommend for getting around the temp file and using a variable instead as requested:

Code: Select all

@echo off
::define a Line Feed (newline) string (normally only used as !LF!)
set LF=^


::Above 2 blank lines are required - do not remove

SETLOCAL ENABLEDELAYEDEXPANSION
set address=%~1
set response=
set positive=
FOR /F "tokens=*" %%a IN ('ping %address%') DO (
   set "response=!response!%%a!LF!"
   echo "%%a" | FIND "Reply from %address%" >nul
   IF !ERRORLEVEL!==0 set positive=YES
)
IF !positive!==YES (
   echo !response!
) ELSE (
   echo ERROR:  do your error handling here
)
GOTO:EOF

The results looks like this:

Code: Select all

C:\Users\Marc\Desktop>pingtest 127.0.0.1
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms

C:\Users\Marc\Desktop>pingtest 128.0.0.1
ERROR:  do your error handling here

I would recommend adding '-n 1' or maybe '-n 2' between the ping and the %address% in the FOR loop to cut down on how long it takes for a bad address to give up, because waiting on 4 failed echo requests is excruciating IMHO :) Otherwise I think this does exactly what you asked for, saving the ping response to a variable and only displaying it onscreen if the response was positive..?

Of course you could filter down the positive output with additional "echo | FIND" logic and FOR loop tokens extraction, so if you wanted to only display the average response time for example you could run through another loop to grab only the line that matches FIND "Average =" and then extract the 7th and 9th token from that line via FOR to get "Average" and "0ms". As they say, the sky's the limit, but I wasn't really sure where you were heading with this so I just printed the whole ping output on success...

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Store whole out put into a string

#4 Post by carlsomo » 08 Aug 2013 22:39

Here is a method to avoid writing to a file:

Code: Select all

@echo off
setLocal enableDelayedExpansion
if "%~1" equ "" goto :pingFailure
set/a line=0
for /f "delims=" %%a in ('ping %~1') do (
    set/a line+=1
    echo(%%a|find "Received = 0" >nul
    if !errorlevel! equ 0 goto :pingFailure
    set "Line#[!line!]=%%a"
)
echo(Ping Output for %~1:
for /l %%a in (1 1 !line!) do echo !line#[%%a]!
goto :eof

:pingFailure
echo(No response
goto :eof

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Store whole out put into a string

#5 Post by carlsomo » 08 Aug 2013 23:39

How about pinging everyone on Earth??

Code: Select all

:Ping the Earth.bat
@echo off
echo(Pinging the Earth starting at: %date%.%time%>"Ping the Earth.log"
for /l %%a in (0 1 255) do (
    for /l %%b in (0 1 255) do (
        for /l %%c in (0 1 255) do (
            for /l %%d in (0 1 255) do (
                call :pingit "%%a.%%b.%%c.%%d">>"Ping the Earth.log"
            )
        )
    )
)
goto :eof

:PingIt
setLocal enableDelayedExpansion
set/a line=0
for /f "delims=" %%a in ('ping %~1') do (
    set/a line+=1
    echo(%%a|find "Received = 0" >nul
    if !errorlevel! equ 0 goto :eof
    set "Line#[!line!]=%%a"
)
echo(&echo(Ping Output for %~1:
for /l %%a in (1 1 !line!) do echo !line#[%%a]!
goto :eof

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Store whole out put into a string

#6 Post by Adrianvdh » 09 Aug 2013 01:21

Thanks, I will try the code and no that is my original post :).

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Store whole out put into a string

#7 Post by penpen » 09 Aug 2013 13:35

I had read your post too fast, and thought you are:
- using the ping command one time with >nul and on success another time to display the result.
- searching for a solution with a variable or a temp file.

Sorry for that :oops: ,
penpen.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: Store whole out put into a string

#8 Post by Adrianvdh » 10 Aug 2013 07:14

PenPen, do't worry its OK :). We all make mistakes sometimes :).

Post Reply