I have a simple batch file that currently all it does it tries to connect to a wifi network up to 10 times.
My trouble is that I am getting an unexpected output in my command prompt when it is ran.
Here is my batch script
setlocal EnableExtensions EnableDelayedExpansion
echo.
echo.
:startSetVariables
set timeout="2"
set PINGS="4"
set pingtimeout="1000"
set endonfail="10"
set wifissid="ROGAR Production"
set /A COUNTER=1
:connectToTheInternet
echo checking internet connection
echo.
:connectToTheInternetRestart
ping -n 1 host | find /i "TTL=" >NUL && (
echo Internet connection already established, skipping to activation!
echo.
goto endNoShutdown
::toupd
) || (
::if failed endonfail times, give up
if "%COUNTER%"=="%endonfail%" (
echo Error: Could not connect to network %endonfail% times, stopping script.
echo.
goto endNoShutdown
)
::raise waiting time between connections
if "%COUNTER%"=="3" (
echo failed 2 times in a row. Increasing wait time between actions to 4 seconds.
echo.
set timeout=4
set /A COUNTER=COUNTER+1
) else if "%COUNTER%"=="5" (
echo failed 4 times in a row. Increasing wait time between actions to 6 seconds.
echo.
set timeout=6
set /A COUNTER=COUNTER+1
) else if "%COUNTER%"=="7" (
echo failed 6 times in a row. Increasing wait time between actions to 8 seconds.
echo.
set timeout=8
set /A COUNTER=COUNTER+1
) else if "%COUNTER%"=="9" (
echo failed 8 times in a row. Increasing wait time between actions to 10 seconds.
echo.
set timeout=10
set /A COUNTER=COUNTER+1
) else (
echo Wait time is currently %timeout% seconds.
set /A COUNTER=COUNTER+1
)
echo Attempt #%COUNTER%.
::disconnect existing network
netsh wlan disconnect
netsh wlan delete profile name="%wifissid%"
timeout /t 1 /nobreak
echo.
::attempt connection
netsh wlan add profile filename="connection.xml"
netsh wlan connect name="%wifissid%" ssid="%wifissid%"
timeout /t %timeout% /nobreak
::check connection
set internet=false
Ping google.com -n 1 -w %pingtimeout%
if errorlevel 1 (
set internet=Failed
) else (
set internet=Connected
)
::check pings
ping -n 1 host | find /i "TTL=" >NUL && (
echo Successfully connected! Starting activation check.
echo.
goto endNoShutdown
::toupd
) || (
echo Connection attempt failed. Starting attempt #%COUNTER% in 3 seconds.
echo.
timeout /t 3 /nobreak
cls
goto connectToTheInternetRestart
)
)
:endShutdown
echo Releasing wifi, shutting down computer.
netsh wlan delete profile name="%wifissid%"
pause
shutdown /s
:endNoShutdown
echo Releasing wifi, ending without shutdown.
netsh wlan delete profile name="%wifissid%"
I believe every opening parentheses has a partner closing parentheses, but my output says otherwise.
Here is my output (I've disabled @ECHO OFF to see where the error actually starts. (o) stands for actual output):
>set /A COUNTER=1
>echo checking internet connection
checking internet connection (o)
>echo.
Ping google.com -n 1 -w 1000
Ping request could not find host google.com. Please check the name and try again. (o - should return errorlevel 1, meaning the following should resolve to else)
>echo Internet access: Failed
Internet access: Failed (o)
>echo.
) was unexpected at this time. (o)
> ) || (
Then the script ends without anything else.
Your
::
comments aren't really comments. They're labels. But labels don't belong inside parenthetical code blocks. Userem
instead, and that should get rid of your) was unexpected at this time
problem.You might also consider condensing your script with a
for /L
loop -- something like this, for example: