Computing.Net > Forums > Programming > help with an easy batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

help with an easy batch file

Reply to Message Icon

Name: doit
Date: December 6, 2007 at 06:26:28 Pacific
OS: Windows XP Proffesional
CPU/Ram: Intel - 1.50 GB RAM
Product: Pentium D
Comment:

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...
pause

this 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



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: December 6, 2007 at 06:39:08 Pacific
Reply:

[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



0

Response Number 2
Name: doit
Date: December 6, 2007 at 07:50:28 Pacific
Reply:

it worked thanks :D

Hobby Programmer


0

Response Number 3
Name: doit
Date: December 6, 2007 at 07:52:39 Pacific
Reply:

it worked thanks :D

Hobby Programmer


0

Response Number 4
Name: klint
Date: December 6, 2007 at 11:13:23 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: help with an easy batch file

Help with the Dos batch File www.computing.net/answers/programming/help-with-the-dos-batch-file/18299.html

needing help with an if command www.computing.net/answers/programming/needing-help-with-an-if-command/19984.html

need help with a simple batch file www.computing.net/answers/programming/need-help-with-a-simple-batch-file/12199.html