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 initial if
/else
structure, and thus removes the need for a goto
.
@Echo Off
SetLocal EnableExtensions
%SystemRoot%\System32\timeout.exe /T 300
For /L %%G In (1,0,10) Do (
%SystemRoot%\System32\netsh.exe WLAN Connect SSID="xxxx" Name="xxxx" && (
%SystemRoot%\System32\choice.exe /C sna /D n /T 30 /M "Text"
If ErrorLevel 3 (
Echo Choice A
)
If Errorlevel 2 (
Echo Choice N
)
If ErrorLevel 1 (
Echo Choice S
)
) || (
Echo Error
)
)