首页 > 解决方案 > 批处理脚本 - 否则无法正常工作

问题描述

@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

命令行输出: 案例 1 - 这工作正常。在Log_output1.txt文件中,存在错误。

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>

案例 2 - 这是行不通的。其中Log_output2.txt文件不包含任何错误 ** if 和 else 条件都被打印出来**

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>

标签: batch-fileif-statement

解决方案


要按照您的方式执行此操作,您需要指示您的代码不要流入下一个标签,如果您不希望它这样做的话。

@Echo Off
SetLocal EnableExtensions

%__AppDir__%findstr.exe /I ^
 /C:"Not connected" ^
 /C:"Invalid command" ^
 /C:"530 Login incorrect" ^
 "D:\PATH1\Log_output#.txt" >NUL 2>&1

If %ErrorLevel% Equ 0 GoTo :Error
Echo Error is not present
Rem Your other commands if no error match was found go here.
GoTo Continue

:Error
Echo Error is present
Rem Your other commands if an error match was found go here.

:Continue
Rem Any other commands to run in both scenarios below here.

尽管您可以使用Call而不是GoTo

@Echo Off
SetLocal EnableExtensions

%__AppDir__%findstr.exe /I ^
 /C:"Not connected" ^
 /C:"Invalid command" ^
 /C:"530 Login incorrect" ^
 "D:\PATH1\Log_output#.txt" >NUL 2>&1

If %ErrorLevel% Equ 0 (
    Call :Error
) Else If %ErrorLevel% Equ 1 Call :NoError

Rem Any other commands to run in both scenarios below here.

Rem Any other commands to run in both scenarios above here.
GoTo :EOF

:Error
Echo Error is present
Rem Your other commands if an error match was found go here.
Exit /B

:NoError
Echo Error is not present
Rem Your other commands if no error match was found go here.
Exit /B

但是,您可以使用错误级别将命令块括起来:

@Echo Off
SetLocal EnableExtensions

%__AppDir__%findstr.exe /I ^
 /C:"Not connected" ^
 /C:"Invalid command" ^
 /C:"530 Login incorrect" ^
 "D:\PATH1\Log_output#.txt" >NUL 2>&1

If %ErrorLevel% Equ 1 (
    Echo Error is not present
    Rem Your other commands if no error match was found go here.
) Else (
    Echo Error is present
    Rem Your other commands if an error match was found go here.
)
Rem Any other commands to run in both scenarios below here.

或使用条件执行:

@Echo Off
SetLocal EnableExtensions

%__AppDir__%findstr.exe /I ^
 /C:"Not connected" ^
 /C:"Invalid command" ^
 /C:"530 Login incorrect" ^
 "D:\PATH1\Log_output#.txt" >NUL 2>&1 && (
    Echo Error is present
    Rem Your other commands if an error match was found go here.
) || (
    Echo Error is not present
    Rem Your other commands if no error match was found go here.
)
Rem Any other commands to run in both scenarios below here.

推荐阅读