Simple batch for checking internet connectivity and setting environment variable %internet

前端 未结 4 669
无人共我
无人共我 2020-12-18 05:16

I want to check internet connection, when it fails I want to set %internet% to not connected. And if it works to connected.

echo ch         


        
4条回答
  •  忘掉有多难
    2020-12-18 05:34

    This worked for me. The idea is it checks each line of the PING result for the line starting with "Reply" (which indicates success) and if it finds it on a single line then it sets the %Connected% variable to true.

    @ECHO OFF
    
    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 the internet.
    ) ELSE (
        SET Internet=Not connected to the internet.
    )
    

提交回复
热议问题