Batch ERRORLEVEL ping response

前端 未结 13 1797
走了就别回头了
走了就别回头了 2020-12-14 23:05

I\'m trying to use a batch file to confirm a network connection using ping. I want to do batch run and then print if the ping was successful or not. The problem is that it a

13条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 23:52

    I 'm not exactly sure what the interaction between FIND and setting the error level is, but you can do this quite easily:

    @echo off
    for /f %%i in ('ping racer ^| find /c "(0%% loss)"') do SET MATCHES=%%i
    echo %MATCHES%
    

    This prints 0 if the ping failed, 1 if it succeeded. I made it look for just "0% loss" (not specifically 4 pings) so that the number of pings can be customized.

    The percent sign has been doubled so that it's not mistaken for a variable that should be substituted.

    The FOR trick serves simply to set the output of a command as the value of an environment variable.

提交回复
热议问题