呼叫或“不打电话"批处理文件? [英] To "Call" or "Not to Call" a batch file?

查看:51
本文介绍了呼叫或“不打电话"批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您从bat文件中调用了另一个批处理文件,但是仍然需要完成一些剩余的操作,那么如何确保对第一个bat文件的调用在完成或出错后会返回到调用它的文件中首先?

If from inside a bat file you called another batch file but still had a few remaining operations to complete, how can you make sure that the call to first bat file will after completion or error, will return to the file that called it in the first instance?

示例:

CD:\MyFolder\MyFiles
Mybatfile.bat

Copy afile toHere

CD:\MyFolder\MyFiles
CALL Mybatfile.bat

COPY afile toHere

使用CALLSTART或根本不使用它们有什么区别?这对是否返回复制命令的结果有影响吗?

What is the difference between using CALL or START or none of them at all? Would this have any impact on whether it would return for the results of the copy command or not?

推荐答案

正如其他人所说,CALL是在.bat中调用另一个bat文件并返回到调用者的常规方法.

As others have said, CALL is the normal way to call another bat file within a .bat and return to the caller.

但是,如果CALLed批处理文件出现致命语法错误,或者CALLed脚本以EXIT终止而没有/B选项,则所有批处理文件处理都将停止(控制权不会返回给调用者).

However, all batch file processing will cease (control will not return to the caller) if the CALLed batch file has a fatal syntax error, or if the CALLed script terminates with EXIT without the /B option.

如果您通过CMD命令执行第二个脚本,则可以保证控制权将返回给调用者(只要控制台窗口保持打开状态).

You can guarantee control will return to the caller (as long as the console window remains open of course) if you execute the 2nd script via the CMD command.

cmd /c "calledFile.bat"

但这有一个局限性,即被调用的批处理设置的环境变量在返回时将不会保留.

But this has a limitation that the environment variables set by the called batch will not be preserved upon return.

我不知道有一个好的解决方案来保证在所有情况下都可以退货并保留环境变化.

I'm not aware of a good solution to guarantee return in all cases and preserve environment changes.

如果在使用CMD时确实需要保留变量,则可以让被调用"脚本将变量更改写入临时文件,然后让调用者读取临时文件并重新建立变量.

If you really need to preserve variables while using CMD, then you can have the "called" script write the variable changes to a temp file, and then have the caller read the temp file and re-establish the variables.

这篇关于呼叫或“不打电话"批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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