繁体   English   中英

在Windows批处理文件中,您可以链式执行*不是*另一个批处理文件吗?

[英]In a Windows batch file, can you chain-execute something that is *not* another batch file?

我知道如果你有两个.bat.cmd文件,我们称之为foobar ,以下规则适用:

没有call

:: Welcome to foo.bat
@bar.bat
@echo We never get to this line because bar.bat is "chain-executed".

call

:: Welcome to foo.bat
@call bar.bat
@echo This line is executed after bar.bat returns.

我的问题是:是否有执行相反的操作,即确保 -batch文件可执行文件链接的方法吗?

:: Welcome to foo.bat
@chain bar.exe
@echo Even though bar is now an .exe,  we never get to this line.
@echo At least, that would be the case if the "chain" command really existed.

换句话说,有没有办法在最后一个例子中执行虚构chain命令的功能?

必须使用命令start在单独的进程中运行可执行文件,并另外退出当前批处理或整个命令进程。

@echo off
echo Welcome to %~nx0
start "Title" bar.exe & exit /B
echo Even though bar is now an .exe, we never get to this line.

此批处理文件在一个单独的进程中启动bar.exeTitle为窗口标题,如果可执行文件是在这种情况下打开的新控制台窗口的控制台应用程序。

然后在start完成后exit /B由命令处理器无条件执行,而bar.exe在单独的进程中运行,导致终止当前批处理文件的处理。

如果未使用命令调用从另一个批处理文件中调用此批处理文件,则命令处理器现在完成处理批处理文件,导致退出命令处理,但使用带有选项/K cmd.exe调用批处理文件以保留命令提示符窗口完成批处理后打开,默认情况下不是这种情况。

但是如果通过从另一个批处理文件调用调用此批处理文件,则只需处理该子批处理文件,并且命令处理器继续处理父批处理文件,而bar.exe在单独的进程中运行。

@echo off
echo Welcome to %~nx0
start "Title" bar.exe & exit
echo Even though bar is now an .exe, we never get to this line.

在此批代码的命令出口是不选项/B这导致终止命令处理之后start起动完成bar.exe在单独的过程,即使当前批处理文件从用呼叫另一个批处理文件称为即使批处理文件处理是以带参数/K cmd.exe开头。

而不是无条件地连接两个命令开始退出操作员&也可以使用如下所示的两个变量块。

只需退出当前批处理:

@echo off
echo Welcome to %~nx0
(
    start "Title" bar.exe
    exit /B
)
echo Even though bar is now an .exe, we never get to this line.

退出整个命令进程:

@echo off
echo Welcome to %~nx0
(
    start "Title" bar.exe
    exit
)
echo Even though bar is now an .exe, we never get to this line.

退出当前批处理或整个命令处理的应用程序的这种启动当然仅在bar.exe启动时才有意义,这取决于批处理文件中的至少一个条件。

注1:
也可以使用goto :EOF而不是exit /B来结束当前的批处理。

注2: goto :EOF如果命令是批处理子程序的一部分,即退出子程序, goto :EOFexit /B导致退出子程序,即使用call :label的标签下面的代码,因为批处理子程序就像嵌入的子批处理文件一样关于批处理的主批文件中。

更多示例演示了callexit /B的行为:

Test1.bat

@echo off
echo Running %~nx0
call Test2.bat
echo Finished %~nx0

Test2.bat

@echo off
echo Running %~nx0
Test3.bat
echo Finished %~nx0

Test3.bat

@echo off
echo Finished %~nx0

从命令提示符窗口中运行Test1.bat导致输出:

Running Test1.bat
Running Test2.bat
Finished Test3.bat
Finished Test1.bat

因此, Finished Test2.bat行丢失了,因为命令处理器从Test3.bat直接返回到Test1.bat

接下来我们编译以下C代码来控制应用程序Test.exe

#include <stdio.h>

int main (int argc, char* argv[])
{
    if(argc > 1)
    {
        printf("Running %s with argument %s\n",argv[0],argv[1]);
    }
    else
    {
        printf("Running %s without an argument\n",argv[0]);
    }
    return 0;
}

我们在以下两个批处理文件中使用Test.exe

Test4.bat

@echo off
echo Running %~nx0
Test.exe 4
call Test5.bat
echo Finished %~nx0

Test5.bat

@echo off
echo Running %~nx0
Test.exe 5
Test.exe 6 & exit /B
echo Finished %~nx0

从命令提示符窗口中运行Test4.bat导致输出:

Running Test4.bat
Running Test.exe with argument 4
Running Test5.bat
Running Test.exe with argument 5
Running Test.exe with argument 6
Finished Test4.bat

因此,线Finished Test5.bat缺少因为命令处理器从执行返回Test.exe用争论6直接Test4.bat

但是使用bar & exit /B然而,如果bar是具有文件扩展名batcmd的批处理文件,或者是具有文件扩展名execom的可执行文件,则仍然很重要。 这可以通过将Test2.bat代码Test2.bat为:

@echo off
echo Running %~nx0
Test3.bat & exit /B
echo Finished %~nx0

从命令提示符窗口中运行Test1.bat导致输出:

Running Test1.bat
Running Test2.bat
Finished Test3.bat

因此,与exit /B中第二批文件所附,命令处理器解释在第二批文件作为第一个批处理文件的上下文出口退出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM