I'm writing a Windows batch file which starts an executable file depending on the status of the wifi connection, and a user choice.
The script uses an infinite loop with a for.
echo off
SETLOCAL EnableDelayedExpansion
timeout /t 300
for /l %%n in (1,0,10) do (
netsh wlan connect ssid="xxxx" name="xxxx"
if errorlevel 1 (
echo error
)
choice /c sna /d n /t 30 /m "Text"
if errorlevel 3 (
echo Choice A
exit
) else if errorlevel 2 (
echo choiche N
) else if errorlevel 1 (
echo choiche S
) else if errorlevel 0 (
echo Default
exit
)
)
After the first if, that checks the connection is established, I would like to insert a goto to skip the choice part and go directly to the end of the loop and restart the iteration.
I tried to put a simple goto end and label :end but the batch doesn't work properly.
echo off
SETLOCAL EnableDelayedExpansion
timeout /t 300
for /l %%n in (1,0,10) do (
netsh wlan connect ssid="xxxx" name="xxxx"
if errorlevel 1 (
echo error
goto end <<<<-----------------------
)
choice /c sna /d n /t 30 /m "Text"
if errorlevel 3 (
echo Choice A
exit
) else if errorlevel 2 (
echo choiche N
) else if errorlevel 1 (
echo choiche S
) else if errorlevel 0 (
echo Default
exit
)
:end
)
I don't know if it's a matter of environment variables or something else.
What about using conditionals, e.g.
&& (echo success) || echo fail, for the first command. This has the effect of reversing your initialif/elsestructure, and thus removes the need for agoto.