batch-file - 批处理脚本 - 否则无法正常工作
问题描述
@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>
解决方案
要按照您的方式执行此操作,您需要指示您的代码不要流入下一个标签,如果您不希望它这样做的话。
@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.
推荐阅读
- powershell - 我可以在最后没有换行符的powershell脚本中写警告吗?
- git - 无法克隆回购 - 没有这样的文件或目录 (tmp_pack_XXXXXX)
- docker - HTTP 请求在邮递员中工作,但不适用于 docker 容器中的 axios
- sql - 将数组列转换为 Presto 中的 K、V 对表
- database - 在 GridDB 中,可以反转索引/行键/搜索吗?
- javascript - 将道具传递给 ReferenceArrayField 似乎不起作用
- java - 黑客等级 Java 字符串令牌
- docker - Gitlab CI/CD 到 Digital Ocean 使用 docker-compose 进行多个 repos
- reactjs - 存在返回时,反应中的导出模块不允许我导入
- python - 如何分隔 .txt 文件的列?.txt 到 .csv 熊猫脚本将所有列合二为一?