jeudi 2 juillet 2020

Batch Script - Else is not Working properly


@echo on

::findstr /C:"Not connected" /C:"Invalid command" /C:"530 Login incorrect" D:\PATH1\Log_output1.txt

findstr /C:"Not connected" /C:"Invalid command" /C:"530 Login incorrect" D:\PATH1\Log_output2.txt

set /a _retVal=%errorlevel%

if %_retVal%==0 (
    goto Error
) else if %_retVal%==1 (
    goto NoError
) else (
    echo Do Nothing!
)
rem *********************************

:NoError
echo Error is not present

:Error
echo Error is present

Command Line Output: Case 1 - This is working fine. In Log_output1.txt file, errors are present. D:\PATH1\scripts\ppt>test1.bat

D:\PATH1\scripts\ppt>findstr /C:"Not connected" /C:"Invalid command" /C:"530 Login incorrect" D:\PATH1\Log_output1.txt Invalid command. Invalid command. Not connected. Not connected. Not connected. Not connected.

D:\PATH1\scripts\ppt>set /a _retVal=0

D:\PATH1\scripts\ppt>if 0 == 0 (goto Error ) else if 0 == 1 (goto NoError ) else (echo Do Nothing! )

D:\PATH1\scripts\ppt>echo Error is present

Error is present

D:\PATH1\scripts\ppt>

Case 2 - This is not WORKING. where Log_output2.txt file doesn't contain any error ** Both if and else conditions are getting printed** D:\PATH1\scripts\ppt>test1.bat

D:\PATH1\scripts\ppt>findstr /C:"Not connected" /C:"Invalid command" /C:"530 Login incorrect" D:\PATH1\Log_output2.txt

D:\PATH1\scripts\ppt>set /a _retVal=1

D:\PATH1\scripts\ppt>if 1 == 0 (goto Error ) else if 1 == 1 (goto NoError ) else (echo Do Nothing! )

D:\PATH1\scripts\ppt>echo Error is not present

Error is not present

D:\PATH1\scripts\ppt>echo Error is present

Error is present

D:\PATH1\scripts\ppt>

Aucun commentaire:

Enregistrer un commentaire