0

I have a .bat file which runs four python files. I need to have some error handling in this .bat file. Basically if any thing goes wrong I want to stop the process.

--Want to stop the process when any of that python script fails. --Want to run as normal when none of the python fails.

python "C:\EarthSQL-Testing\mckayplod.py" "C:\EarthSQL-Testing"
IF %ERRORLEVEL% EQ 0  GOTO :Next
IF %ERRORLEVEL% NEQ 0 ECHO PYTHON ERROR HAS FOUND ->Error.txt
Copy "C:\EarthSQL-Testing\Error.txt"  "C:\Users\EarthSQL\EarthSQL\Documents - Documents\ErrorLog\" /y
PAUSE

:Next
python "C:\EarthSQL-Testing\mckaypersonnel.py" "C:\EarthSQL-Testing"
IF %ERRORLEVEL% EQ 0  GOTO :Next
IF %ERRORLEVEL% NEQ 0 ECHO PYTHON ERROR HAS FOUND ->Error.txt
Copy "C:\EarthSQL-Testing\Error.txt"  "C:\Users\EarthSQL\EarthSQL\Documents - Documents\ErrorLog\" /y
PAUSE

:Next
python "C:\EarthSQL-Testing\mckaydrilling.py" "C:\EarthSQL-Testing"
IF %ERRORLEVEL% EQ 0  GOTO :Next
IF %ERRORLEVEL% NEQ 0 ECHO PYTHON ERROR HAS FOUND ->Error.txt
Copy "C:\EarthSQL-Testing\Error.txt"  "C:\Users\EarthSQL\EarthSQL\Documents - Documents\ErrorLog\" /y
PAUSE

:Next
python "C:\EarthSQL-Testing\mckactivities.py" "C:\EarthSQL-Testing"
IF %ERRORLEVEL% EQ 0  GOTO :Next
IF %ERRORLEVEL% NEQ 0 ECHO PYTHON ERROR HAS FOUND ->Error.txt
Copy "C:\EarthSQL-Testing\Error.txt"  "C:\Users\EarthSQL\EarthSQL\Documents - Documents\ErrorLog\" /y
PAUSE

Currently the above script, it is running only first python file and then it doesn't run the 2nd python file and onwards files. Any help what is wrong in my batch file ????

xyz
  • 11
  • 1
  • 2
    Please open a [command prompt](https://www.howtogeek.com/235101/), run `if /?` and read the output usage help. There is explained already on first output help page how the exit code of an executable or command can be evaluated with an __IF__ condition with valid and always working syntax `EQ` is not a valid comparison operator which can be seen on the error message output on [debugging a batch file](https://stackoverflow.com/a/42448601/3074564) I recommend to read [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Nov 27 '21 at 21:56
  • It explains the usage of the conditional operators `&&` for exit code is __equal__ `0` indicating a success of the execution of previous executable/command __and__ `||` for exit code __not equal__ `0` which is used by most programs for a not 100% successful execution. There is also explained how to use, for example, `if not errorlevel 1 goto Next` to continue batch file processing on the line below the label line `:Next` on `python.exe` exited with `0` for success (or a negative value which no programmer should ever user in a program or script for an exit value). – Mofi Nov 27 '21 at 22:03
  • BTW: It is possible to use multiple times the same label in a batch file. But it is advisable to use unique labels in a batch file, i.e. use `Next1`, `Next2`, `Next3`, ... and `goto Next1`, `goto Next2`, `goto Next3`, ... or more meaningful labels. So use `python.exe "C:\EarthSQL-Testing\mckayplod.py" "C:\EarthSQL-Testing" && goto Next1`, next the command lines executed on execution of Python script `mckayplod.py` exited with a value greater 0 and then the label line `:Next1`. – Mofi Nov 27 '21 at 22:08

1 Answers1

-1

This may not make a difference but I suggest trying swapping out EQ with == and NEQ with !==!

DylanH2009
  • 13
  • 4
  • That is not useful as simply wrong. Please open a command prompt window and run `if /?`. There is the string comparison operator `==` which can be used here too, but there is no comparison operator `!=` or `!==` or `!==!`. A string comparison on not equal is done with `not "..." == "..."`. See [Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files](https://stackoverflow.com/a/47386323/3074564) for more details about how to do a string comparison with __IF__. – Mofi Nov 29 '21 at 20:08
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 29 '21 at 22:41