我正在尝试通过批处理脚本运行批处理文件,当批处理文件失败时,我想打印一条错误,就像Sync failed
它通过一样,我想打印一条消息Sync success
因此,当我的脚本失败时:它会打印,Sync failed
但是当我的脚本(get_files.bat)成功时它会打印:
Sync failed
Sync success
所以我不知道为什么它会打印两者Sync failed
以及Sync success
当我的脚本(get_files.bat)成功通过时
我的代码:
echo Sync Starts
cd C:\Users\Common\Files
call get_files.bat
if errorlevel 1 goto ERROR
echo SUCCESSFUL
:ERROR
echo Failed
cmd /k
exit /b 1
答案1
您可以在脚本末尾添加 EOF 标签,当脚本运行时转到 EOF
echo Sync Starts
cd C:\Users\Common\Files
call get_files.bat
if errorlevel 1 goto ERROR
echo SUCCESSFUL
goto EOF
:ERROR
echo Failed
cmd /k
exit /b 1
:EOF
或者你可以使用 0 代码退出
echo Sync Starts
cd C:\Users\Common\Files
call get_files.bat
if errorlevel 1 goto ERROR
echo SUCCESSFUL
exit /b 0
:ERROR
echo Failed
cmd /k
exit /b 1
答案2
@echo off
echo\Sync Starts & cd /d "C:\Users\Common\Files"
call "get_files.cmd"|findstr /be 0 >nul && goto %:^) || goto %:^(
%:^)
:: your code/commands come here to refer to an SUCCESSFUL event
timeout 3 | exit /b 0 | echo\SUCCESSFUL & goto :EOF
%:^(
:: your code/commands come here to refer to an Failed event
timeout 3 | echo\If you see this, your "get_files.cmd" Failed
exit /b 1 | goto :EOF
您需要对齐蝙蝠,以便一个蝙蝠的交互能够将执行结果通知给另一个蝙蝠,仅goto :label/:eof
在调用另一个蝙蝠的执行上编辑是没有用的,它不会返回成功发生的准确执行,一个或多个错误。
您的脚本旨在在以下情况下采取行动returning 0
或者returning non 0
用于执行其他文件批处理,但要知道它对 bat 文件不太适用,call | if !errorlevel!
您将无法获得与从可执行文件(或内部/外部命令)获得的错误级别相同的准确返回值,这也适用“部分地”,对于某些 cmd.exe/在块中执行(like dir file_1.txt file2.txt)
。
您的命令解释器(cmd.exe
)将逐个命令地处理该可执行文件的返回/错误级别,在某些情况下,以块()为单位。
为一个.cmd|.bat
文件,cmd.exe将分别处理每个执行,并且您将获得return 0
或return non 0
,仅适用于此last command
get_files.bat中的
关于
errorlevel
在.bat
vs.cmd
文件中使用...Old style .bat Batch files vs .cmd Batch scripts. There is a key difference between the way .CMD and .BAT batch files set errorlevels
来源链接ss64.comA .BAT batch script running the 'new' internal commands: APPEND, ASSOC, PATH, PROMPT, FTYPE and SET will only set ERRORLEVEL if an error occurs. So if you have two commands in the batch script and the first fails, the ERRORLEVEL will remain set even after the second command succeeds.
为了给出执行状态的精确状态,您可以采取的措施是将
bat
变量发送到另一个批次,或者至少发送输出中的某些字符串,从而替换错误级别,但它需要您的批次的一个版本与执行更加一致,以响应发起执行调用的批次,无论是否对错误事件做出预测性响应。
@echo off
set "_error=0"
command_normal 1
command_normal 2
command_normal 3
command_critical 1 || set "_error=1"
command_critical 2 || set "_error=2"
command_critical 3 || set "_error=3"
command_normal 4
.....
command_normal n
.....
command_critical n || set "_error=n"
exit /b | echo\%_error%
@echo off
echo\Sync Starts
cd /d "C:\Users\Common\Files"
call "get_files.cmd"|findstr /be 0 >nul && goto :Next_CMDs || goto :Error
:Next_CMDs
:: your code/commands come here to refer to an SUCCESSFUL event
exit /b 0 | echo\SUCCESSFUL & goto :EOF
:Error
:: your code/commands come here to refer to an Failed event
exit /b 1 | echo\Failed & goto :EOF
- 使用:
call file.bat/.cmd
使用findstr /begin /end
和0
与运算符&&
和/或||
编辑你的bat,使用操作符来控制命令的结果||
( ),你可以根据每个结果return non 0
定义自己的errolevel
“关键执行”,还利用exit /b
命令与你预定义的错误级别,它将被调用的蝙蝠捕获,并且相关操作将准确执行。
"get_files.cmd" | findstr /be 0 && goto :Next_CMDs
your_command (if return 0) run this command too
call "get_files.cmd" | findstr /be 0 || goto :ERRORs
your_command (if return non 0) run this command too
- 使用:
&&(commands) ||(commands)
your_command | findstr /be 0 && (
return 0
rem :: more command here
rem :: more command here
rem :: ...
) || (
return non 0
rem :: more command here
rem :: more command here
rem :: ...
)
其他资源: