Solved what is wrong with this (if-else) execution in batch?

July 6, 2012 at 11:45:55
Specs: Windows XP
I am testing some small scripts that I have. This particular one is driving me crazy. In my mind it should work, but it is not doing what I thought it would/should.


@echo off
echo testing the issue
echo.

ECHO Checking the log file for errors...
echo %errorlevel%
FINDSTR /C:"open failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking1)

:TSTErrChecking1
FINDSTR /C:"does not exist" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking2)

:TSTErrChecking2
FINDSTR /C:"Logon failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking3)

:TSTErrChecking3
FINDSTR /C:"Failure" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking4)

:TSTErrChecking4
FINDSTR /C:"RC (return code) = 0" C:\automatic_deployment\test1.txt
echo %errorlevel%
IF %ERRORLEVEL% == 0 (
ECHO Deployment was successful.
pause
GOTO quit) ELSE (
ECHO Deployment failed.
pause
GOTO quit)


:quit
exit


As you can see I am using "FINDSTR" to find certain words/strings from a small test1.txt file and act accordingly. I want it to say "deployment failed" and quit asap if it sees any "fail"/ "does not exist"/"open failed". If it cannot find anyone of those above and finds the "RC (return code) = 0" I want it to say it was "successful".

So, i put all those test words and strings in my test1.txt file and tested the batch script, but it keeps skipping (i think) all those fail hints/words and keeps saying it was successful. Plz, help. Thanks in advance.


See More: what is wrong with this (if-else) execution in batch?

Report •


#1
July 6, 2012 at 14:23:05
✔ Best Answer
Maybe its uppercase/lowercase problem? If so, you might try:
findstr /i /c:"fail"
to override case-restrictions.
Otherwise, it should work, although you could make it alot shorter using "findstr /g:failtest.txt" and put all your fail strings into that file (but not the rc=0, do that test separately), something like:

findstr /i /L /g:failtest.txt logfile && (
echo failure to deploy
goto quit
)
findstr /i /c:"rc=0" logfile && (
echo successful deploy
goto quit
) || (
echo unknown failure, rc not zero
)
:quit

(failtest.txt content:)
does not exist
Logon failed
Failure


Report •
Related Solutions


Ask Question