I am not Here
I am not Here

Reputation: 59

Checking internet connection and if yes, do an action with batch file

I have this script:

@echo off
Ping www.google.nl -n 1 -w 1000 >nul 2>nul
if errorlevel 1 (set internet=Nao conectado) else (set internet=Connectado) >nul 2>nul

echo %internet%

    if %internet%=Nao conectado goto 1
    if %internet%=Conectado goto 2

    :1 exit

    :2
    echo hi
    pause

But the condittionals if %internet% isn't working. What is wrong?

Upvotes: 0

Views: 42

Answers (2)

Compo
Compo

Reputation: 38589

Instead of needlessly setting variables, I suppose you could simplify it:

@Echo Off
Ping www.google.nl -n 1 -w 1000 >Nul 2>&1
If ErrorLevel 1 Exit /B
Echo hi
Pause

Or:

@Echo Off
Ping www.google.nl -n 1 -w 1000 >Nul 2>&1|| Exit /B
Echo hi
Pause

Upvotes: 2

Squashman
Squashman

Reputation: 14290

Two things wrong with your IF commands.

1) If you read the help file you will notice that the syntax for a string comparison requires the use of two equals symbols.

2) If you need to compare strings that have spaces you need to enclose your comparisons with quotes.

if "%internet%"=="Nao conectado" goto 1

Upvotes: 2

Related Questions