-1

I asked yesterday about changing the color in command prompt based on a ping result. A user was kind enough to supply me with the script below.

The script works great except for one problem. When the connection times out, it does nothing. It just sort of pauses. How would I update this script to include a variable or something that will also change the text if a time out is reported?

@setlocal enableextensions enabledelayedexpansion
@echo off
set /p ipaddr="Enter ip or url: eg localhost or google.co.za or 192.168.0.1"
set /p cutoff="enter minimum good reply ms: eg 300 "
:loop
for /f "tokens=7,9" %%a in ('ping -n 1 !ipaddr!') do (
  if "%%a"=="Average" (
  set a=%%b
  set /a res=!a:~0,-2!
  )
)

if %res% LEQ %cutoff% (
    COLOR 2
    echo %ipaddr% responded in %res%ms
)else (
    COLOR 4
    echo %ipaddr% responded in %res%ms
)
ping -n 3 127.0.0.1 >nul: 2>nul:
goto :loop

endlocal
Compo
  • 36,585
  • 5
  • 27
  • 39
nbarr7655
  • 23
  • 5
  • Possible duplicate of [Change Color in Command Prompt Based on Result?](https://stackoverflow.com/questions/37148086/change-color-in-command-prompt-based-on-result) – Emile Bergeron Jan 18 '18 at 19:07

1 Answers1

0

There is no "Average", when there is no response, so your variables keep unchanged (show the last "valid" value)

Simply add set res=No_Response between :loop and for .... to preset the variable before the ping.

Stephan
  • 53,940
  • 10
  • 58
  • 91