Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

file - Simple batch for checking internet connectivity and setting environment variable %internet% depending on result

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...