Batch file with If-Then-Else Logic

February 20, 2011 at 17:43:56
Specs: Windows XP
Concept:
I am repeatedly (not continuously) pinging an IP address and want to record the date and time of ONLY the failing occurrences. Then wait 5 minutes and repeat the program until I press Crtl-C to end. I am not concerned with the passing results.

Example:
Pinging 192.168.10.11 with 32 bytes of data:
Request timed out.
Etc.

At the moment I have the PING results sent to a file named C:\RESULTS.TXT.
I can query C:\RESULTS.TXT for the word "Request" fine, but here is where I get stuck.
Based on the results of the query, if the word "Request" is found I want to append the current TIME and DATE to a file named FAIL.TXT, wait 5 minutes and repeat from the beginning.
If the C:\RESULTS.TXT file does not contain "Results" then just wait 5 minutes and repeat from the beginning.

My guess is something like this:
:begin
ping 192.168.11.11 > C:\RESULTS.TXT
find /c "Request" C:\RESULTS.TXT

rem ****
rem **** My Problem (not sure of the syntax, but this is the idea):
If “Request” is not found
goto wait

If “Request” is found

rem ****
rem **** I'm OK from here until :Wait
Date > c:\Date.text
Time > c:\Time.txt
Copy c:\date.txt + C:\Time.txt > Fail.txt

goto Wait
:Wait
rem ****
rem **** another problem
somehow count to 5 minutes (300 seconds)
Goto begin

Any assistance is greatly appreciated.
Paul


See More: Batch file with If-Then-Else Logic

Report •


#1
February 20, 2011 at 20:54:50
:: ==========================================
::
:: pingfail.bat Mon 21-02-2011 11:37:04.13
@echo off & setLocal enableDELAYedeXpansion

:loop
ping 192.168.11.11 | Find /i "reply" > nul
if errorlevel 1 >> log echo %DATE% %TIME%
ping 1.1.1.1 -n 1 -w 300000 > nul
goto :loop


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
February 20, 2011 at 22:40:52
Paul, you should replace LOG with FAIL.TXT.. above script will output the failed timestamp in a file name "log".

Subhash Chandra.


Report •

#3
February 21, 2011 at 09:25:59
Mechanix2Go
This is my finished product.

subhash chandra
Thanks for the tip regarding "log". I changed it to: log.txt

Many thanks to both of you. I learned a lot during this exercise. This solution was a lot simpler than my original one would have been.

============
@echo Please enter IP Address to be tested.
@echo off
Set /P IPTEST=
@echo %IPTEST% Is this correct? Ctrl-C to Exit or
Pause

:loop
SET /A COUNT=COUNT+1
@ECHO Test #%COUNT%

@echo off
ping %IPTEST% | Find /i "Reply" > nul
if errorlevel 1 >> log.txt echo No Reply %IPTEST%, %DATE% %TIME%

goto Wait
:Wait
ping 1.1.1.1 -n 1 -w 300000 > nul

Goto :loop
============


Report •

Related Solutions

#4
February 21, 2011 at 21:59:07
GTH,

Since you're learning:

[1] You only need to echo off once.

[2] You can simplify your prompting and setting to:

Set /P IPTEST=which IP ?

[3] No point in GOing TO the very next line.


=====================================
Life is too important to be taken seriously.

M2


Report •


Ask Question