# Writing if/else batch file



## Legacy777

There may be an easier way to do this, and if there is, I'm all ears.

But this is what I'd like to do. I want to create a batch file that I can run, and in this batch file it pings a website, google.com for example. It then looks through the output text, if it finds "Lost = 4" it writes "failed" and a date/time stamp to a separate text file. If it finds "Received = 4" it writes "success" and a date/time stamp to that same text file.

I know to do this will involve an if else loop, and a few other things, but I'm just not 100% sure how to put it all together.

I essentially want to keep tabs on my internet connection. I'm having sporadic connectivity issues to my webmail server during the day. It seems to happen mostly during the middle of the day, and I need to know if I'm loosing internet connectivity, or if I'm having some other issue.

Thanks in advance for any help.

Josh


----------



## MacFromOK

This is not going to happen with a DOS batch file, unless you can find some batch utility that can search a file with the ping results and set an errorlevel or a var if your search string is found.

What OS do you have, or is this strictly a DOS system?

Cheers, Mac


----------



## Legacy777

Yeah I sort of figured that's where things wouldn't work out too easily.

OS is win2k. I wanted to use a batch file, and call it every few minutes using the task scheduler. So if there is a win utility that will do what I'm looking to do....that'd work too


----------



## MacFromOK

I'm on 98SE so I have no clue about 2k - does it have the DOS "ping" utility?


----------



## MacFromOK

My point is, ya might try just scheduling it to run and redirect the result to a file - then use a text editor occasionally to search for your string. I assume this is just a temporary test thingy isn't it?


----------



## MacFromOK

Or try a google search for "free ping utility" (include the quotes).

Here's one free util that showed up:

http://www.advtoolware.com/t4e/fp/fp_default.htm

"FREEping is a free ping utility which will ping all your 2003/XP/2000/NT servers (or any other IP address) in free-definable intervals. FREEping will send you a popup when one of the 2003/XP/2000/NT servers stops responding."

Cheers, Mac


----------



## Squashman

http://www.snapfiles.com/get/pinglog.html


----------



## Legacy777

Yeah this is just a temp thing.

Thanks for the suggestions guys. I'll play with the ping utilities, and if they don't do what I want, I'll try your suggestion about just going through the file manually.

Josh


----------



## pr0t3st

Legacy777 said:


> There may be an easier way to do this, and if there is, I'm all ears.
> 
> But this is what I'd like to do. I want to create a batch file that I can run, and in this batch file it pings a website, google.com for example. It then looks through the output text, if it finds "Lost = 4" it writes "failed" and a date/time stamp to a separate text file. If it finds "Received = 4" it writes "success" and a date/time stamp to that same text file.


Try this:



Code:


@ECHO OFF
setlocal

SET FAIL="Lost = 4"
SET RECD="Received = 4"

FOR /F "tokens=1,2,3 delims=/ " %%I IN ('DATE /T') DO SET date1=%%I/%%J/20%%K
FOR /F "tokens=1,2 delims=: " %%I IN ('TIME /T') DO SET time1=%%I:%%J

ping 127.0.0.1 | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  failure >>pingstat.log& GOTO :EOF

ping 127.0.0.1 | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  success >>pingstat.log& GOTO :EOF

endlocal

This "basic" CMD/BAT script should accomplish the task... It's been awhile since I've written batch scripts, so it isn't perfect as it should be... A few things worthy of mention:

The " >> " appends to the logfile - pingstat.log. If you want the logfile to be recreated everytime or overwritten each time the script is run, change ">>pingstat.log" to ">pingstat.log" (w/o quotes).


----------



## Legacy777

Thanks a bunch pr0t3st!

That does exactly what I want. The date output was a little screwy, so I fixed that. I also added another line in there for "unknown host" in case dns craps out.

Here's the final code.



Code:


@ECHO OFF
setlocal

SET UNKN="Unknown host"
SET FAIL="Lost = 4"
SET RECD="Received = 4"

FOR /F "tokens=1,2,3,4 delims=/ " %%I IN ('DATE /T') DO SET date1=%%J/%%K/%%L
FOR /F "tokens=1,2 delims=: " %%I IN ('TIME /T') DO SET time1=%%I:%%J

ping google.com | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  unknown >>pingstat.log& GOTO :EOF

ping google.com | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  failure >>pingstat.log& GOTO :EOF

ping google.com | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time%]  success >>pingstat.log& GOTO :EOF

endlocal

thanks again for the help.
Josh


----------



## Legacy777

One last change.



Code:


@ECHO OFF
setlocal

SET UNKN="Unknown host"
SET FAIL="Lost = 4"
SET RECD="Received = 4"

FOR /F "tokens=1,2,3,4 delims=/ " %%I IN ('DATE /T') DO SET date1=%%J/%%K/%%L
FOR /F "tokens=1,2 delims=: " %%I IN ('TIME /T') DO SET time1=%%I:%%J

ping google.com | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time[b]1[/b]%]  unknown >>pingstat.log& GOTO :EOF

ping google.com | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time[b]1[/b]%]  failure >>pingstat.log& GOTO :EOF

ping google.com | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time[b]1[/b]%]  success >>pingstat.log& GOTO :EOF

endlocal

Output is now this



Code:


[04/18/2005 1:19p]  success

compared to what it was originally



Code:


[04/18/2005 13:16:11.00]  success


----------

