Calling a BAT file from another .bat file

I have a .bat file to which I can pass parameters.

LOAD_TABLE_WRAPPER.BAT Table1 DEV 

In short, it runs SQL to load Table1 into the Dev environment. Now I want him to load several tables in one night. So, I created a .BAT wizard that looks something like

 ::MASTER_LOAD.BAT CALL LOAD_TABLE_WRAPPER.BAT Table1 Dev CALL LOAD_TABLE_WRAPPER.BAT Table2 Dev CALL LOAD_TABLE_WRAPPER.BAT Table3 Dev 

If I send MASTER_LOAD.BAT from cmd, it will load for Table1, but will not proceed to load Table2. These are the last two lines of WRAPPER.BAT

 :eof exit %ERROR_STATUS% 
+6
source share
4 answers

Your exit %error_status% in LOAD_TABLE_WRAPPER.BAT ends your batch session, so your MASTER_LOAD.BAT will never be able to resume from the next call.

You can fix the problem by simply adding the / B option to the EXIT command

 exit /b %error_stats% 

I almost never use EXIT without / B in a batch file (although there are times when / B is not needed).

But another alternative is to run called scripts via CMD instead of CALL.

 ::MASTER_LOAD.BAT CMD /C LOAD_TABLE_WRAPPER.BAT Table1 Dev CMD /C LOAD_TABLE_WRAPPER.BAT Table2 Dev CMD /C LOAD_TABLE_WRAPPER.BAT Table3 Dev 

There are a number of differences between the methods.

CALL with EXIT / B

  • Relatively fast
  • Can save values ​​of environment variables upon return (SETLOCAL is available if you do not want to save values)
  • A script is called that inherits pending extensions and extension states (enabled or disabled)

CMD / C

  • Relatively slow
  • Cannot save environment variable values ​​after return. (You can write the definitions to a file and load them back after returning to the wizard, but this is not convenient or efficient).
  • The script invoked always starts with delays with the extension and extension by default (normal slow extension is disabled and extensions are enabled)

I would never recommend using CMD / C over CALL if the called batch files do not have EXIT without the / B option, and you cannot change the batch file to add the / B option.

+5
source

You must use the start command to solve this problem. A call causes the current process to run commands. You can learn more about the launch here. The main implementation should be:

 "START program-name arg1 arg2"` 
0
source

Is there a problem when executing CALL LOAD_TABLE_WRAPPER.BAT Table1 Dev? What is the result when you echo between calls?

0
source

try giving the PAUSE command, for example:

CALL LOAD_TABLE_WRAPPER.BAT Table1 Dev PAUSE CALL LOAD_TABLE_WRAPPER.BAT Table2 Dev PAUSE CALL LOAD_TABLE_WRAPPER.BAT Table3 Dev

-1
source

Source: https://habr.com/ru/post/922069/


All Articles