1

Having a latency less than 600 but greater than 90 continues to go to :FAST CONNECTION. It should be going to :MODERATE CONNECTION.

@ECHO off
MODE CON:cols=38 lines=11
:LOOP
SET a=3000
FOR /f "tokens=7 delims== " %%G IN ('PING -4 -n 1 8.8.8.8^| FIND "TTL" ') DO SET a=%%G
CLS
IF %a% EQU 3000 (GOTO :NO CONNECTION) ELSE (GOTO :SPEED)

:SPEED
IF %a% GTR 600 (GOTO :SLOW CONNECTION)else (IF %a% LSS 90 (GOTO :FAST CONNECTION) else (GOTO :MODERATE CONNECTION))
TIMEOUT /T 2 > NUL

:NO CONNECTION
COLOR 4F
ECHO.
ECHO.
ECHO     --- NO CONNECTION ---
ECHO.
ECHO  CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
CLS
ECHO.
ECHO.
ECHO     *** NO CONNECTION ***
ECHO.
ECHO  CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
CLS
ECHO.
ECHO.
ECHO    !!! NO CONNECTION !!!
ECHO.
ECHO  CHECK YOUR NETWORK CONNECTION
TIMEOUT /T 2 > NUL
GOTO :END

:FAST CONNNECTION
COLOR 2F
ECHO.
ECHO         YOU CURRENTLY HAVE A
ECHO   FAST CONNECTION TO INTERNET: %a%
ECHO.
ECHO   APPLICATIONS AND FILE TRANSFERS
ECHO       WILL RUN AT A GREAT RATE
ECHO.
ECHO           FAST     0 - 10
ECHO       MODERATE     11 - 20
ECHO           SLOW     600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END


:MODERATE CONNECTION
COLOR 6F
ECHO.
ECHO        YOU CURRENTLY HAVE A
ECHO        MODERATE CONNECTION TO THE INTERNET : %a%
ECHO.
ECHO        APPLICATIONS AND FILE TRANSFERS
ECHO        WILL RUN AT A SO/SO RATE
ECHO.
ECHO        FAST 0 - 10
ECHO        MODERATE 11 - 20
ECHO        SLOW 600 - 3000
TIMEOUT /T 3 > NUL
GOTO :END

:SLOW CONNECTION
COLOR 4F
ECHO.
ECHO        YOU CURRENTLY HAVE A
ECHO        SLOW CONNECTION TO INTERNET: %a%
ECHO.
ECHO        ALTERNATE OR CONTINGENCY
ECHO        NETWORK WILL RUN AT A SLOWED RATE
ECHO.
ECHO        FAST 0 - 10
ECHO        MODERATE 11 - 20
ECHO        SLOW 600 - 3000

TIMEOUT /T 3 > NUL
GOTO :END

:END
GOTO :LOOP
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Labels should not contain a space character. [Debug your batch file](https://stackoverflow.com/a/42448601/3074564) and you can see that and read help of command __GOTO__ by opening a [command prompt](https://www.howtogeek.com/235101/), running `goto /?` and reading the output help. I recommend to read also DosTips forum topic: [ECHO. FAILS to give text or blank line - Instead use ECHO/](https://www.dostips.com/forum/viewtopic.php?f=3&t=774) – Mofi Aug 10 '20 at 07:01
  • 2
    On my German Windows machine the string `TTL` is assigned to environment variable `a` which is obviously not an integer number. Output of `%SystemRoot%\System32\ping.exe` depends on language. – Mofi Aug 10 '20 at 07:07
  • You should add `<` (and `>`?) to the list of delimiters because of replies like `time<1ms`. But anyway, `1ms` is not an integer, so your `if` conditions would probably still fail. And there should be a _space_ before `else`… – aschipfl Aug 10 '20 at 07:59
  • 3
    Your entire concept is wrong, the time in milliseconds for a ping response from a host does not provide a measurable indication of the speed of your internet connection. – Compo Aug 10 '20 at 13:25

3 Answers3

0

FOR /f "tokens=7 delims== " %%G IN ('PING -4 -n 1 8.8.8.8^| FIND "TTL" ') DO SET a=%%G

On a US-EN machine a gets set to a number with the time unit ms appended e.g. 100ms.

This causes the following comparisons to work as string comparisons (lexicographically), not number comparisons, so for example:

C:\etc>if 100ms LSS 11 @echo ???
???

C:\etc>if 100ms LSS 10 @echo ???

C:\etc>

The quick-and-dirty solution in this case is to strip the ms suffix right after the for loop.

set "a=%a:~0,-2%"

Note: this may not - and will likely not - work on localized Windows other than US-EN.

dxiv
  • 16,984
  • 2
  • 27
  • 49
  • 3
    is `set a=%a:ms=%` not better? – Gerhard Aug 10 '20 at 08:09
  • @Gerhard That's another way to do it. Whether better or not is a matter of preference. – dxiv Aug 10 '20 at 15:16
  • 1
    So, if the OP shares the batch file with his friend, his friend has a different keyboard launguage setup and does not have `ms` in the variable, what will happen? `:)` – Gerhard Aug 10 '20 at 15:21
  • 1
    @Gerhard It will fail, of course, just as the last line in my post says. But attempting to specifically remove "ms" won't work, either, since it will leave in place whatever other text suffix may be there. It will just fail in a different way. – dxiv Aug 10 '20 at 15:44
0

For the purpose of this demonstration, I've set a timeout of 3 seconds at the end of the script, you can adjust this as you please.

Your main issue is that you are using labels with spaces, you cannot do that. Second issue is that, as already mentioned by @Mofi in a comment, depending on your keyboard settings (language) different items are assigned to %a%.

I however suggest that you do 2 pings and use the average result, instead of ttl.

As a side note, ping (using ICMP) is for diagnostics and will not always get priority.

@echo off
MODE CON:cols=38 lines=11
:test
set latency=3000
@echo off
for /f "tokens=2delims==, " %%i in ('ping -4 -n 2 8.8.8.8^| find /i "average" ') do set "latency=%%i"
    set latency=%latency:ms=%
    if %latency% gtr 600 if %latency% lss 3000 (
       color 4F
       echo(
       echo Slow connection %latency%
       echo(
  )
   if %latency% geq 3000 (
       color 4F
       echo(
       echo Connection seems down %latency%
       echo(
  )
   if %latency% lss 90 (
       color 2F
       echo(
       echo Fast Connection %latency%
       echo(
  )
   if %latency% gtr 90 if %latency% lss 600 (
       color 6F
       echo(
       echo Moderate Connection %latency%
       echo(
  )
(timeout /t 3)>nul 2>&1 & cls & goto test

You will note, for obvious reasons I removed some of the noise you had, you can add all the echo( back if you want, but I do not see the purpose of it. I also removed all the goto labels, as it is not required.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

Please begin by taking note of my comment, which tells you that this code will not provide a measurable indication of your internet connection's speed.

Now, without radically changing your script and layout, you could probably use with Win32_PingStatus, instead on . This should ensure that your variable always has the response time in milliseconds, if one is returned within the allowed timespan:

Example, (Just change the Host Address on line 5 as needed):

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Mode CON:Cols=45 Lines=11

Set "HA=8.8.8.8"

:Loop
Color
Set "RT="
For /F EOL^=R %%G In ('%__AppDir__%wbem\WMIC.exe Path Win32_PingStatus Where^
 "Address='%HA%'" Get ResponseTime 2^>NUL')Do For %%H In (%%G)Do Set "RT=%%H"
If Not Defined RT GoTo None
If %RT% Gtr 600 GoTo Slow
If %RT% Gtr 90 GoTo Moderate

:Fast
Color 2F
Echo=
Echo             YOU CURRENTLY HAVE A
Echo     FAST CONNECTION TO INTERNET: %RT%ms
Echo=
Echo       APPLICATIONS AND FILE TRANSFERS
Echo          WILL RUN AT A GREAT RATE
Echo=
Echo             FAST     : 0 - 10
Echo             MODERATE : 11 - 20
Echo             SLOW     : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop

:Moderate
Color 6F
Echo=
Echo             YOU CURRENTLY HAVE A
Echo MODERATE CONNECTION TO THE INTERNET: %RT%ms
Echo=
Echo       APPLICATIONS AND FILE TRANSFERS
Echo          WILL RUN AT A SO/SO RATE
Echo=
Echo             FAST     : 0 - 10
Echo             MODERATE : 11 - 20
Echo             SLOW     : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop

:Slow
ClS
Color 4F
Echo=
Echo             YOU CURRENTLY HAVE A
Echo     SLOW CONNECTION TO INTERNET: %RT%ms
Echo=
Echo       ALTERNATE OR CONTINGENCY NETWORK
Echo          WILL RUN AT A GREAT RATE
Echo=
Echo             FAST     : 0 - 10
Echo             MODERATE : 11 - 20
Echo             SLOW     : 600 - 3000
Echo=
%__AppDir__%timeout.exe /T 5 /NoBreak >NUL
GoTo Loop

:None
ClS
Color 4F
Echo=
Echo            --- NO CONNECTION ---
Echo=
Echo        CHECK YOUR NETWORK CONNECTION
Echo=
%__AppDir__%timeout.exe /T -1
GoTo Loop

Please be aware, that I do not know why you've asked the code to loop back to the ping, so if that's not your overall intention, for now you'll need to use CTRL+C to stop if from looping.

Compo
  • 36,585
  • 5
  • 27
  • 39