从另一个或从提示调用 Windows 批处理文件的几种方法。哪种情况下哪个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2607856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:15:19  来源:igfitidea点击:

Several ways to call a windows batch file from another one or from prompt. Which one in which case?

windowsbatch-fileshell

提问by dim

A windows batch file (called.bator called.cmd) can be called from another batch file (caller.bator caller.cmd) or interactive cmd.exe prompt in several ways:

可以通过多种方式从另一个批处理文件(或)或交互式 cmd.exe 提示符调用Windows 批处理文件(called.batcalled.cmd):caller.batcaller.cmd

  1. direct call: called.bat
  2. using call command: call called.bat
  3. using cmd command: cmd /c called.bat
  4. using start command: start called.bat
  1. 直接调用: called.bat
  2. 使用调用命令: call called.bat
  3. 使用cmd命令: cmd /c called.bat
  4. 使用启动命令: start called.bat

I'm quite in trouble to differentiate their intended usage based on their help text: when to use which one? e.g. why I might use 'call' command instead of direct call. What's different?

我很难根据他们的帮助文本区分他们的预期用途:什么时候使用哪个?例如,为什么我可能会使用“调用”命令而不是直接调用。有什么不同?

I'm interested on some summary report that analyze all 4 possibilities (and others if any missing) from various point of views: recommended use cases for which they are designed to fit, process spawning, execution context, environment, return code processing.

我对一些总结报告很感兴趣,这些报告从不同的角度分析了所有 4 种可能性(如果有的话,还有其他可能性):推荐的用例,它们旨在适合、进程生成、执行上下文、环境、返回代码处理。

Note: I'm using Windows XP SP3.

注意:我使用的是 Windows XP SP3。

回答by Kyle Alons

  1. The batch file will be executed by the current cmd.exe instance (or a new cmd.exe instance if, for instance, double-clicked in Explorer).

  2. Same as #1, only has an effect when used inside a batch/cmd file. In a batch file, without 'call', the parent batch file ends and control passes to the called batch file; with 'call' runs the child batch file, and the parent batch file continues with statements following call.

  3. Runs the batch file in a new cmd.exe instance.

  4. Start will run the batch file in a new cmd.exe instance in a new window, and the caller will not wait for completion.

  1. 批处理文件将由当前的 cmd.exe 实例(或新的 cmd.exe 实例,例如,如果在资源管理器中双击)执行。

  2. 与#1 相同,仅在批处理/cmd 文件中使用时才有效。在批处理文件中,没有“调用”,父批处理文件结束,控制权传递给被调用的批处理文件;with 'call' 运行子批处理文件,父批处理文件继续调用后的语句。

  3. 在新的 cmd.exe 实例中运行批处理文件。

  4. Start 将在新窗口中的新 cmd.exe 实例中运行批处理文件,调用者不会等待完成。

回答by ken

One thing not clear from the comments here: When you call one batch file from another by using just its name (Case #1 in the original question), execution stopsfrom the calling batch file. For example, in these lines:

从这里的评论中不清楚一件事:当您仅使用其名称(原始问题中的案例#1)从另一个批处理文件调用一个批处理文件时,执行会从调用批处理文件停止。例如,在这些行中:

called.bat
echo Hello

The 'echo Hello' line (and anything following it) will not be called. If you use the 'call' keyword, execution resumes after the call. So in this case:

'echo Hello' 行(及其后的任何内容)将不会被调用。如果您使用“call”关键字,则在调用后继续执行。所以在这种情况下:

call called.bat
echo Hello

The 'echo Hello' line will be called.

'echo Hello' 行将被调用。

Additionally, all the variables set in the called.bat file will be passed along back to the calling process, too.

此外,在调用的.bat 文件中设置的所有变量也将传递回调用进程。

Imagine a 'called.bat' file that had this line:

想象一个有这一行的“被调用的.bat”文件:

set MYVAR=hello

Then, %MYVAR% would be available to the calling batch file if it used:

然后, %MYVAR% 可用于调用批处理文件,如果它使用:

call called.bat

But, it would not be using

但是,它不会使用

REM starts a new cmd.exe process
start called.bat   

REM stops and replaces current cmd.exe process with a new one
called.bat