从另一个.bat文件中调用.BAT文件 [英] Calling a .BAT file from another .bat file

查看:337
本文介绍了从另一个.bat文件中调用.BAT文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.bat文件,可以向其传递参数.

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

LOAD_TABLE_WRAPPER.BAT Table1 DEV

简而言之,它运行SQL以在Dev环境上加载Table1.现在,我希望它在一夜之间加载多个表.因此,我设置了一个主.BAT文件,其内容类似于

Briefly, it runs an SQL to load Table1 on the Dev environment. Now, I want it to load multiple tables overnight. So, I set up a master .BAT which goes 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

如果我从cmd提交MASTER_LOAD.BAT,它将执行表1的加载,但不会继续进行表2的加载.这是WRAPPER.BAT

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

:eof
exit %ERROR_STATUS%

推荐答案

您在LOAD_TABLE_WRAPPER.BAT中的exit %error_status%命令正在终止您的批处理会话,因此您的MASTER_LOAD.BAT将永远没有机会继续下一次调用.

Your exit %error_status% command in LOAD_TABLE_WRAPPER.BAT is terminating your batch session, so your MASTER_LOAD.BAT never gets a chance to resume with the next call.

您只需在退出命令中添加/B选项即可解决该问题

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

exit /b %error_stats%

我几乎从不在批处理文件中使用没有/B的EXIT(尽管有时不希望使用/B).

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

但是另一种选择是通过CMD而不是CALL运行被调用的脚本.

But another alternative is to run the 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

通过退出/B拨打电话

  • 比较快
  • 可以在返回时保留环境变量值(如果您不想保留值,则可以使用SETLOCAL)
  • 被调用的脚本继承了延迟的扩展和扩展状态(启用或禁用)

CMD/C

  • 比较慢
  • 返回时无法保留环境变量值. (您可以将定义写到文件中,然后在返回主文件时将其加载回去,但这既不方便也不高效)
  • 被调用脚本始终以默认的延迟扩展和扩展状态开始(通常禁用延迟扩展并启用扩展)

我永远不建议在CALL上使用CMD/C,除非被调用的批处理文件具有不带/B选项的EXIT并且您不能修改批处理文件以添加/B选项.

I would never recommend using CMD /C over CALL unless the called batch files have EXIT without the /B option and you cannot modify the batch file to add the /B option.

这篇关于从另一个.bat文件中调用.BAT文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆