Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Im making a batch file were the .bat checks which computers that are available on the network, using the command ping.
But i cant seem to get the errorlevels to work with ping.
I want the batch program to say trough echo if the user is found, or is not found.Heres what i tryed:
@Echo off
Color C7
Echo Checking connectivity to other users on network...
Ping \\william
IF ERRORLEVEL 0 GoTo :William
IF ERRORLEVEL 1 GoTo :failedwilliam:william
Echo Found William on the network...
pause:failedwilliam
Echo Couldn't find William on the network...
pausethis is just a small part of my batch program.
The problem is that even if my computer doesnt find \\William, the batch takes the way to :william and says that it found him on the network.Icant get the errorlevel to work with the ping command.
Can some1 please help me?
Hobby Programmer

[1] ping will return 0 whether the box was found or not.
[2] IF ERRORLEVEL 0
means 0 *OR MORE* , so it will always be true.
What you need is:
ping 127.0.0.1 | find "Reply" > nul
if errorlevel 1 goto :failed
goto :OK
=====================================
If at first you don't succeed, you're about average.M2

Actually, ping returns errorlevel 1 either if you have made a usage error (which you have!) or if the remote host doesn't respond.
Also, as M2 says, you shouldn't put "if errorlevel 0" because that would branch for all possible error levels, which is pointless.
The ping command doesn't take \\hostname, just hostname.
If you don't want to try 4 times, use ping -n 1 hostname. Type ping -? for help.
The batch commands you've written work for all versions of the command processor, including Windows 98. If you are on Windows XP, you should use more structured techniques (rather than goto) as shown below:
for %%n in (william fred barny) do (
ping -n 1 %%n > nul
if errorlevel 1 (
echo %%n is not responding.
) else (
echo %%n is alive on the network.
)
)You can even shorten it thus:
(ping -n 1 william > nul) && echo william is alive || echo william is not responding

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |