我正在编写一个 Windows 批处理文件,它根据 wifi 连接的状态和用户选择启动一个可执行文件。
该脚本使用带有
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
)
)
在第一个
if
检查连接是否建立之后,我想插入一个 goto
来跳过 choice
部分,直接转到循环末尾并重新开始迭代。
我尝试放置一个简单的
goto end
和标签 :end
但批次无法正常工作。
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
)
不知道是环境变量的问题还是其他的问题。
如何使用条件语句,例如
&& (echo success) || echo fail
,第一个命令。这具有反转您最初的 if
/else
结构的效果,从而消除了对 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
)
)