批处理脚本失败时批处理脚本中的错误处理

批处理脚本失败时批处理脚本中的错误处理

我正在尝试通过批处理脚本运行批处理文件,当批处理文件失败时,我想打印一条错误,就像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 0return non 0,仅适用于此last commandget_files.bat中的

Get_Files.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%

The_Batch_caller.bat

@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 /end0与运算符&&和/或||

编辑你的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 :: ...
                                 )


其他资源:

相关内容