批处理ERRORLEVEL ping响应 [英] Batch ERRORLEVEL ping response

查看:320
本文介绍了批处理ERRORLEVEL ping响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用批处理文件,以确认使用ping的网络连接。我想这样做批处理,然后打印如果ping成功与否。的问题是,当作为批处理运行,它总是显示失败。这里是code:

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 always displays 'failure' when run as a batch. Here is the code:

@echo off
cls
ping racer | find "Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),"
if not errorlevel 1 set error=success
if errorlevel 1 set error=failure
cls
echo Result: %error%
pause

'赛车'是我的计算机的名称。我在我的电脑平本身,所以我可以消除连接不良的变量。正如我之前所说的,该批次总是导致失败。奇怪的是,如果我复制code进入命令提示符程序工作正常。有谁知道为什么程序工作正常,在命令提示符下,但作为批处理不起作用?
谢谢

'racer' is the name of my computer. I'm having my computer ping itself so I can eliminate the variable of a poor connection. As I said before, the batch always results in failure. Oddly enough, the program works fine if I copy the code into the command prompt. Does anyone know why the program works fine in the command prompt but doesn't work as a batch? Thanks

推荐答案

我也不知道是什么东西之间的相互作用FIND 并设置错误水平,但你能做到这一点很容易:

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%

这版画 0 如果ping失败, 1 ,如果它成功了。我使它看起来只是0%的损失(未明确4坪),这样坪数可以自定义。

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.

这篇关于批处理ERRORLEVEL ping响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆