batch to automatically ping IPs

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
batchn00b
Posts: 1
Joined: 10 Nov 2011 13:09

batch to automatically ping IPs

#1 Post by batchn00b » 10 Nov 2011 13:22

Hi,
New to the forum, and new to the batch world.
Looking for any help I can get. I am attempting to build a batch file that will ping a list of IPs, and I would like it to automatically start pinging the IP's I have listed in another file.

This is what I have so far:


Code: Select all

ping -a -n 1 %1 |findstr "Reply from" >> ping.log
echo %ERRORLEVEL%
if errorlevel==1 goto notgood
if errorlevel==0 goto good
:notgood
echo %1 is not pingable >> ping.log
goto end
:good
echo %1 is good >> ping.log
goto end
:end
echo ---------------------- >> ping.log



Is there a way to open a batch file, have it automatically change the directory to the Desktop, and run the one command to execute the batch file?

This is the command that I run to run the batch file

Code: Select all

for /f %z in (pingip.txt) do pinghosts.bat %z

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: batch to automatically ping IPs

#2 Post by phillid » 29 Dec 2011 19:35

Welcome to the world of Batch scripting :)

We certainly can change to the 'desktop' directory! :D

Just use the following command:

Code: Select all

cd %userprofile%\Desktop

This should work no matter what user is logged on, and IIRC, it should work on Windows 7 and Vista too

This is because Windows dynamically assings a value to the %userprofile% variable for each session that's opened.
Try logging on as different users and running

Code: Select all

echo %userprofile%

if you feel like digging around and experimenting

After changing to the Desktop using the CD command, just put

Code: Select all

for /f %z in (pingip.txt) do pinghosts.bat %z

on the next line and run the batch file :)

--Phillid

Squashman
Expert
Posts: 4470
Joined: 23 Dec 2011 13:59

Re: batch to automatically ping IPs

#3 Post by Squashman » 30 Dec 2011 08:40

Just wondering why you are using one batch file to call another batch file when this could all easily be done in one batch file?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: batch to automatically ping IPs

#4 Post by aGerman » 30 Dec 2011 09:04

Further more there is a major fault in the IF statements.
if errorlevel==1 goto notgood

This compares the string errorlevel with 1 which can never be "true".

Either use the variable %errorlevel% ...

Code: Select all

if %errorlevel%==1 goto notgood

... or use the old errorlevel syntax ...

Code: Select all

if errorlevel 1 goto notgood

... the meaning is: if the errorlevel value equals or is greater than 1.
(BTW "if errorlevel 0 ..." would be always useless in case of PING :wink:)

Regards
aGerman

Post Reply