我正在开发一个 ping 批处理程序。我想学习的是当连接失效时如何跳过循环?例如:
@echo off
:1
ping www.stackoverflow.com
(I guess) if errorlevel 1 set errorlev=1
If errorlevel 1 goto 2
goto 1
:2
Echo ping failed!
Title ping failed!
color c
我正在开发一个 ping 批处理程序。我想学习的是当连接失效时如何跳过循环?例如:
@echo off
:1
ping www.stackoverflow.com
(I guess) if errorlevel 1 set errorlev=1
If errorlevel 1 goto 2
goto 1
:2
Echo ping failed!
Title ping failed!
color c
你可以试试这个:
:back
ping www.stackoverflow.com >nul && goto:back
Echo ping failed!
所以你想永远 ping 直到 ping 失败,然后停止?
@echo off
:1
ping -n 1 www.stackoverflow.com | find "Reply from">nul2>nul && goto :1
echo ping failed!
title ping failed!
color c
注意:我通过管道查找而不是检查 ERRORLEVEL,因为某些 Windows 版本与为某些故障条件设置 ERRORLEVEL 不一致。请参见下面的示例。
C:\Users\Me>ping -n 1 192.168.1.2
Pinging 192.168.1.2 with 32 bytes of data:
Reply from 192.168.1.102: Destination host unreachable.
Ping statistics for 192.168.1.2:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
C:\Users\Me>echo %errorlevel%
0