简单的批处理检查互联网连接和设置环境变量%上网%依赖于结果 [英] 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------------
这更多的是code的。
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.
-----------------------------编辑3 ---------------- - 结果
完成
-----------------------------edit 3 ------------------
DONE
推荐答案
通过一个调整,原来的code会工作。
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%
问题是, IF ERRORLEVELñ
为TRUE,如果错误级别
比N N 或大于。 IF ERRORLEVEL 0
因此总是如此。 IF NOT ERRORLEVEL 1
是错误级别= 0的测试。那么, IF%ERRORLEVEL%= = 0
,但前者可以在块内使用,但后者不能。
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.
禁欲,你的code的已的设置未连接
但下面的行,因为如果
的条件总是真,覆盖与第二个消息的价值。
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
.
此外,在一个块语句(一parenthesised一系列语句)
,在全部块被解析和然后执行。任何%VAR%
块内将在块被解析的时间变量的值的替换 - 程序被执行前 - 同样的道理也适用于一个 FOR ... DO(块)
。
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)
.
这样,因为你正在设置网络
与块(即如果 - 真
语句序列,那么你需要要使用的两种常见的方法来克服这一点。1)使用 SETLOCAL enabledelayedexpansion
和场所使用!无功!
的%VAR%
访问 VAR
或2)调用子程序的更改值使用进行进一步的处理更改后的值。技术上涉及到第二个方法是使用的语句调用回声%%互联网%%
显示互联网改变价值
块内。
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.
这篇关于简单的批处理检查互联网连接和设置环境变量%上网%依赖于结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!