CALL 命令与 START with/WAIT 选项

如何使用带有 WAIT 选项的 START 命令

START /wait notepad.exe
START /wait  notepad.exe

和使用 CALL 命令有什么不同吗?

CALL notepad.exe
CALL notepad.exe

是否存在这样一种情况,一个人的行为可能与另一个人的不同,这取决于正在执行的是什么?

597805 次浏览

对于 前任文件,我认为差异几乎不重要。
但是开始一个 前任你甚至不需要 CALL

当开始另一批的时候,这是一个很大的区别,
因为 CALL将在相同的窗口中启动它,并且被调用的批处理可以访问相同的变量上下文。
所以它也可以改变影响调用者的变量。

START将为所调用的批处理创建一个新的 cmd.exe,如果没有/b,它将打开一个新窗口。
因为它是一个新的上下文,变量不能被共享。

差异

使用 start /wait <prog>
- 当 <prog>结束时,环境变量的变化将丢失
- 调用者等待,直到 <prog>完成

使用 call <prog>
- 对于 前任它可以被省略,因为它等于刚开始 <prog>
对于 前进,调用者批处理等待或启动 前任异步,但行为取决于 前任本身。
对于 批次文件,调用者批处理继续,当被调用的 <batch-file>结束时,如果没有调用,控件将不会返回到调用者批处理

附录:

使用 CALL可以更改参数(用于批处理文件和 exe 文件) ,但仅当它们包含插入符号或百分号时才能更改。

call myProg param1 param^^2 "param^3" %%path%%

将扩展为(从批处理文件中)

myProg param1 param2 param^^3 <content of path>

我认为他们的表现应该大致相同,但是有一些不同。 START通常用于启动应用程序或启动给定文件类型的默认应用程序。这样,如果你 START http://mywebsite.com它不做 START iexplore.exe http://mywebsite.com

START myworddoc.docx会启动 Microsoft Word 并打开 myworddoc.docx.CALL myworddoc.docx做同样的事情... 然而 START提供了更多的窗口状态和类似的选项。它还允许设置进程优先级和关联。

简而言之,考虑到 start 提供的附加选项,它应该是您的首选工具。

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
[command/program] [parameters]


"title"     Title to display in window title bar.
path        Starting directory.
B           Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
I           The new environment will be the original environment passed
to the cmd.exe and not the current environment.
MIN         Start window minimized.
MAX         Start window maximized.
SEPARATE    Start 16-bit Windows program in separate memory space.
SHARED      Start 16-bit Windows program in shared memory space.
LOW         Start application in the IDLE priority class.
NORMAL      Start application in the NORMAL priority class.
HIGH        Start application in the HIGH priority class.
REALTIME    Start application in the REALTIME priority class.
ABOVENORMAL Start application in the ABOVENORMAL priority class.
BELOWNORMAL Start application in the BELOWNORMAL priority class.
NODE        Specifies the preferred Non-Uniform Memory Architecture (NUMA)
node as a decimal integer.
AFFINITY    Specifies the processor affinity mask as a hexadecimal number.
The process is restricted to running on these processors.


The affinity mask is interpreted differently when /AFFINITY and
/NODE are combined.  Specify the affinity mask as if the NUMA
node's processor mask is right shifted to begin at bit zero.
The process is restricted to running on those processors in
common between the specified affinity mask and the NUMA node.
If no processors are in common, the process is restricted to
running on the specified NUMA node.
WAIT        Start application and wait for it to terminate.

例如,在调用 regsvr32.exe /s时,callstart /wait之间有一个有用的区别,regsvr32.exe /s也被 加里in 引用 在他对“ How-do-i-get-the-application-exit-code-from-a-windows-command-line”的回答中

call regsvr32.exe /s broken.dll
echo %errorlevel%

总是返回0但是

start /wait regsvr32.exe /s broken.dll
echo %errorlevel%

将返回 regsvr32.exe 的错误级别

从另一个 而不停止父批处理程序。调用一个批处理程序 call 命令接受标签作为调用的目标。当在脚本或批处理文件之外使用时,调用在命令行上没有效果。 Https://technet.microsoft.com/en-us/library/bb490873.aspx

开始

启动 单独的命令提示符窗口以运行指定的程序或命令。如果不使用参数,start 将打开第二个命令提示符窗口。 Https://technet.microsoft.com/en-us/library/bb491005.aspx

这是我在并行运行批处理文件时发现的情况(同一 bat 文件的多个实例同时具有不同的输入参数) :

假设您有一个 exe 文件,它执行一个名为 LongRunningTask.exe

如果您直接从 bat 文件调用 exe,那么只有对 LongRunningTask 的第一次调用才会成功,而其余的调用将得到一个 OS 错误“ File is already in use by the process”

如果使用以下命令:

Start/B/WAIT”“ LongRunningTask.exe”

您将能够运行 bat 和 exe 的多个实例,同时仍然等待任务完成,然后 bat 继续执行剩余的命令。B 选项是为了避免创建另一个窗口,为了使命令正常工作,需要使用空引号,参见下面的参考文献。

注意,如果在开始时没有使用/WAIT,则 LongRunningTask 将与批处理文件中的其余命令同时执行,因此如果其中一个命令需要 LongRunningTask 的输出,则可能会产生问题

恢复:

这不能并行运行:

  • 调用 LongRunningTask.exe

这将并行运行,只要命令的输出和 bat 文件的其余部分之间没有数据依赖关系,就没有问题:

  • “启动/B”“ LongRunningTask.exe”“参数”

这将并行运行并等待任务完成,因此您可以使用输出:

  • Start/B/WAIT”“ LongRunningTask.exe”

Start 命令的引用: 如何从批处理文件运行程序而不在程序启动后打开控制台?

这是一个老线程,但我刚刚遇到这种情况,并发现了一个巧妙的方法绕过它。我尝试运行 setup.exe,但焦点是返回到脚本的下一行,而不等待 setup.exe 完成。我尝试了以上的解决方案,但没有成功。

最后,通过管道将命令传递给更多的人起到了作用。

Exe {语法参数} | 更多