Batch File (.BAT) – Wait For Network Connection

If you have a batch file that runs on a system startup and has to connect to some server, you may want to make it wait until the network becomes ready before actually making attempt to connect.

In this note i will show a code snippet that can be used in a batch file to wait for the network connection before executing a task.

Cool Tip: Run a batch file (.BAT) on a system startup! Read more →

Wait For Network Connection in a Batch File

The code snippet below pings the IP address of a Google’s public DNS server to test the connection to the Internet .

If it is not reachable, i.e. there is no Internet, it goes to the Retry block where it waits for 5 seconds before making the next attempt.

Once the IP address becomes reachable, it goes to the ExecTask block and prints the “OK” message.


ping -n 1 %IPADDRESS% | find "TTL=" >nul
if errorlevel 1 (
    goto Retry
) else (
    goto ExecTask

ping -n 6 >nul REM wait for 5 seconds (-n %SECONDS%+1)
goto :TestNetworkConnection

echo "Connection to %IPADDRESS% is OK."

Leave a Reply