I want to check internet connection, when it fails I want to set %internet%
to not connected. And if it works to connected
.
echo checking internet connection
Ping www.google.nl -n 1 -w 1000
cls
if errorlevel 1 (set internet=Not connected to internet)
if errorlevel 0 (set internet=Connected to internet)
I also tried this:
@echo off
cls
echo Checking connection
ping -n 1 www.google.com >nul
if errorlevel 1 (
cls
set "%internet%"=="Not connected to internet"
echo %internet%
pause>nul
exit
)
cls
set "%internet%"=="Connected to internet"
echo %internet%
pause>nul
pause
---------------EDIT------------
This is more of the code.
@echo off
set versienummer=v3.1
title AutoMatic Program Install Stable %versienummer% by eric
color 0a
:CheckOS
IF "%PROCESSOR_ARCHITECTURE%"=="x86" (set bit=x86) else (set bit=x64)
set "windows="
VER | find " 5.1." > nul && set windows=XP
VER | find " 5.2." > nul && set windows=XP 64-Bit or Server 2003 or Server 2003 R2
VER | find " 6.0." > nul && set windows=Vista or server 2008
VER | find " 6.1." > nul && set windows=Win7 or server 2008 R2
VER | find " 6.2." > nul && set windows=Windows 8
VER | find " 6.3." > nul && set windows=Server 2012 R2 or Windows 8.1
if defined windows (
echo %windows%
) else (
echo unknown operating system
)
:ReturnToBaseLine
echo checking internet connection
Ping www.google.nl -n 1 -w 1000
cls
if errorlevel 1 (set internet=Not connected to internet)
if errorlevel 0 (set internet=Connected to internet)
SET Connected=false
FOR /F "usebackq tokens=1" %%A IN (`PING google.com`) DO (
REM Check the current line for the indication of a successful connection.
IF /I "%%A"=="Reply" SET Connected=true
)
REM Check if a success was found.
IF "%Connected%"=="true" SET internet=Connected to internet
REM If we get here, we are not connected.
set internet=Not connected to internet
pause
REM Quit.
color 0a
cls
echo Made By The Amazing
echo.
echo ____ _ ________ ___
echo / __/___(_)___ /_ __/ / ___ / _ \_______ ___ ___ _ ___ ____
echo / _// __/ / __/ / / / _ \/ -_) / // / __/ -_) _ `/ ' \/ -_) __/
echo /___/_/ /_/\__/ /_/ /_//_/\__/ /____/_/ \__/\_,_/_/_/_/\__/_/
echo.
Echo %bit% processor architecture versie %versienummer%
echo %windows%
echo %internet%
echo.
-----------------------------edit 3 ------------------
DONE
To check the internet connection in Flutter, you need to add the connectivity plus plugin and then you can check the internet connection by manually calling its checkConnectivity method or listen to the network connectivity changes by calling its onConnectivityChanged.
With a tweak, your original code will work.
echo checking internet connection
Ping www.google.nl -n 1 -w 1000
cls
if errorlevel 1 (set internet=Not connected to internet) else (set internet=Connected to internet)
echo %internet%
The issue is that IF ERRORLEVEL n
is TRUE if errorlevel
is n or greater than n. IF ERRORLEVEL 0
is therefore always true. IF NOT ERRORLEVEL 1
is a test for errorlevel=0. So is IF %ERRORLEVEL%==0
, except that the former can be used within a block but the latter cannot.
Nence, your code was setting not connected
but the following line, since the if
condition is always true, overwrites the value with the second message.
Note that set "%internet%"=="Connected to internet"
means "set a variable, the name of which is stored in the variable internet
to the value"
Hence, if internet
had a value of fred
at that time, then fred
would be set to the value, not internet
.
Furthermore, Within a block statement (a parenthesised series of statements)
, the entire block is parsed and then executed. Any %var%
within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block)
.
Thus, since you are setting internet
with a block (the if - true
statement sequence, then you need to use one of the two common ways to overcome this. 1) use setlocal enabledelayedexpansion
and use !var!
in place of %var%
to access the changed value of var
or 2) to call a subroutine to perform further processing using the changed values. A method technically related to the second is to use the statement call echo %%internet%%
to display the changed value of internet
within a block.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With