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...