I wanted to use
ping -n 1 8.8.8.8 | findstr /r /c:"[0-9] *ms"
to check if a internet connection is possible, before going forward in my code. But it does not work, when used in a file.
When i enter this command in a new command prompt manually it works as expected. When it is used in a file it gets 'kinda stuck' at this line and does nothing, as if it would wait for something that never happens. Any ideas on what this problem is and/or how to approach it? Thanks
I would advise for this task that the first line of your batch-file looks like this:
@%SystemRoot%\System32\ping.exe -n 1 8.8.8.8 1>NUL || GoTo :EOF
This should end the script at the first line if the ping
command line is unsuccessful.
You may additionally want to consider using a timeout function. If you open a Command Prompt window, type ping /?
and press the ENTER key, you should note that it has a -w
option, which will accept a parameter in milliseconds.