Page 1 of 1

batch file help

Posted: 29 Dec 2010 13:25
by dlboriginals
Hello, I am rather new to creating batch files. And need some help figuring out how to do this function.

I have a batch file that pings a list of ip addresses. If anyone of those ip's are not reachable then it records that information to a log file. This works fine, I also have it logging all the information from the ping requests, this works fine.

I have it then email the badhost.log file to myself if contains " not reachable " responses.

The problem I am having is that its still emails me the badhost.log file even if the file is blank.

How can I create a function or If statement that says:

If the file badhost.log is empty do not email it
else
If the file badhost.log contains information email it


Any help is greatly appreciated.

Thank you

Re: batch file help

Posted: 29 Dec 2010 13:51
by Prezident
You could first search the file with find or findstr for any word you know should be there and check the errorlevel to decide if the email should be sent.

Code: Select all

FIND /i "Destination host unreachable" badhost.log>nul
if errorlevel 0 (
     email log file
) else (
     don't email log file
)


Would that work for you?

Re: batch file help

Posted: 29 Dec 2010 14:14
by dlboriginals
I'm going to try it and see. I appreciate your help and fast response.

The email program I am using in dos is mailsend.exe and I use command line within the batch file to execute the mailsend

I'll let you know if it works.

Re: batch file help

Posted: 29 Dec 2010 14:50
by dlboriginals
Ok that didn't work.

Here's my code below, that I have pieced together from stuff I've found online.

Code: Select all

Rem delete badhost.log & allIP.log files if they exists
If exist badhost.log del badhost.log
If exist allIP.log del allIP.log

Rem create badhost.log file
@echo off > badhost.log

Rem read all the ip addresses in cameras.txt file
for /f %%I in (cameras.txt) do (
ping %%I > %temp%\#
find "Reply" < %temp%\# > nul
if errorlevel 1 echo %%I This Camera is Down >> badhost.log
type %temp%\# >> allIP.log
echo.Complete %%I,
)
Rem find This camera is down in file badhost.log
FIND /i "This Camera is Down" badhost.log > nul
if errorlevel 0 (
     goto :restart   
) else (
     start mailsend -v -f some@email.here -d mailserver here -smtp smtp server here -p 25 -t some@email.here -sub Camera_Status_Report -y text -a d:\badhost.log
)

:restart
echo.
echo.
@echo off
net session /delete /y
set i=3600
for /L %%j in (1 1 3600) do (
call :DisplayProgressBar %i%
ping 0.0.0.0 -n 2 > NUL
set /a i = i - 1
)
cls
:DisplayProgressBar
title Restarting in approx %i% seconds (60 Minutes)
%0



I have reversed the goto statement with the email one but it still emails the log file.

What other options are there?

Re: batch file help

Posted: 29 Dec 2010 16:47
by Prezident
You could try either of the following:

Code: Select all

if errorlevel 1 (
     goto :restart   
) else (
     start mailsend -v -f some@email.here -d mailserver here -smtp smtp server here -p 25 -t some@email.here -sub Camera_Status_Report -y text -a d:\badhost.log
)


Or this:

Code: Select all

if errorlevel 0 (
     start mailsend -v -f some@email.here -d mailserver here -smtp smtp server here -p 25 -t some@email.here -sub Camera_Status_Report -y text -a d:\badhost.log
) else (
     goto :restart
)


In your example you are telling it to email you the file if the string is not found, that's the first operation of the if statement.

if condition == value (
do this based on a true condition
) else (
do this based on a false condition
)


Errorlevel 0 means that whatever your previous operation was succeeded or in your case, the string was found with the find command so it will carry out the first option

Errorlevel 1 would mean that the string was not found and would carry out the first option. It all depends on how you want to detect the errorlevel and the way you structure the IF statement.

I'm probably not as good at explaining this stuff as some of the other people here. I often have to play around with different options when I'm checking for a condition like this.

Re: batch file help

Posted: 29 Dec 2010 17:01
by aGerman
Prezident wrote:if errorlevel 0 (

No. This will always be true, because the meaning is "if errorlevel is 0 or greater than 0".

You could use
if %errorlevel%==0 (
instead.

Regards
aGerman

Re: batch file help

Posted: 29 Dec 2010 20:01
by Prezident
My mistake... See, I still learn new stuff all the time. I've seen it used different ways and never quite understood why some have == and some don't.

Thanks again aGerman. :)

Re: batch file help

Posted: 29 Dec 2010 20:40
by aGerman
Yeah, there is a big difference between the old errorlevel handling and the new (since cmd.exe) %errorlevel% variable.

From the help for IF:

Code: Select all

[...]
IF [NOT] ERRORLEVEL number command
[...]
  ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.
[...]

That means if a command returns different errorlevel values, you have to start with the greatest possible value in your first IF command line.

Later:

Code: Select all

[...]
%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL
[...]


In the end
if errorlevel 1 ...
is the same like
if %errorlevel% geq 1 ...
where geq is the compare-operator for "greater than or equal".

Regards
aGerman