I am trying to run a batch file via a batch script and when the batch file is failing I want to print an error as Sync failed if it passes I want to print a message as Sync success
So when my script fails:
its prints Sync failed
but when my script(get_files.bat) success it prints:Sync failedSync successSo I dont know why its print both Sync failed & Sync success when my script(get_files.bat) passed successfully
My code:
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 2 Answers
You can add a EOF label at the end of the script and when the script works goto 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 or instead, you can exit with 0 code
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 3 @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" Failedexit /b 1 | goto :EOFYou will need to align the bats, so that it is possible for the interaction of one to inform the result of the execution to the other bat, it is no use editing a goto :label/:eof only on the bat that calls the execution of the other, it will not return an accurate/precisely execution of the successful occurrence, one or more errors.
Your script is designed to take actions when returning 0 or returning non 0 for the execution of your other file batch, but understand that it doesn't work well for bat files, with call | if !errorlevel! you will not get an accurate return the errorlevel to the same way that you get from an executable (or internal/external command), which also applies "in part", for some in blocks (like dir file_1.txt file2.txt).
Your command interpreter (cmd.exe), will handle the return/errorlevel of that executable, individually, command by command, a for some cases, in blocks().
For a .cmd|.bat file, the cmd.exe will treat each execution separately, and you will get the return 0 or return non 0, just for the last command in this get_files.bat
About using
errorlevelin.batvs.cmdfile...What you can do to give a precise status of the execution status, would be to send
batvariables to the other batch, or at least some string in the output, thus replacing the errorlevel, but it requires an edition of your bat for one version more aligned with the execution to respond to the bat that originated the execution call, something that responds predictively to the error event or not.
@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- Using:
callusingfindstr /begin /endwith0and operator&&and/or||
Editing your bat to make use of a control over the results of your commands using operator || (return non 0), where you can define your own errolevel based on the result of each "critical execution", also making use of the exit /b command with your errorlevel predefined, which will be captured by the calling bat and the relevant actions will be performed accurately.
"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- Using
&&(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 :: ... )Additional resources: