windows - 在Windows批处理文件中,您可以链式执行不是另一个批处理文件的内容吗?

标签 windows batch-file cmd

我了解您是否有两个.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.

我的问题是:是否可以执行相反的操作,即确保非批处理文件可执行文件被链接?
:: 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.exe作为窗口标题启动Title

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

如果未使用命令从另一个批处理文件中调用该批处理文件,则命令处理器现在将完成该批处理文件的处理,从而退出命令处理,除了使用cmd.exe/K选项调用该批处理文件以保持命令提示符窗口处于打开状态在完成批处理之后,默认情况下不是这样。

但是,如果从另一个批处理文件中使用调用调用了该批处理文件,则仅此子批处理文件的处理完成,并且命令处理器继续处理父批处理文件,而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.

在此批处理代码中,命令 exit 是不带/B选项的,即使在另一个批处理文件中,使用调用调用了当前批处理文件,并且即使批处理文件处理,start在单独的进程中完成了bar.exe的启动后,也会终止命令处理。以参数cmd.exe/K开头。

除了将两个命令 start exit 与运算符&无条件连接起来,还可以使用下面所示的两个变量块。

仅退出当前的批处理:
@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 :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.exe6直接返回到Test4.bat

但是,使用bar & exit /B还是很重要的,如果bar是具有文件扩展名batcmd的批处理文件,或者是具有文件扩展名execom的可执行文件。这可以通过将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的情况下,命令处理器将第二个批处理文件中的退出解释为第一个批处理文件的上下文中的退出。

关于windows - 在Windows批处理文件中,您可以链式执行不是另一个批处理文件的内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34229259/

相关文章:

batch-file - 从 for 循环中的变量中减去特定字符串

python - 属性错误 Cmd 模块

c# - 如何从SSIS调用cmd.exe并在命令行执行命令

windows - Aptana Studio 3 中的 Git 存储库在哪里?

c++ - 显示如何向 Opera/Firefox 4 等 Vista 窗框的玻璃部分添加按钮/菜单的示例

node.js - npm 在 Windows 中不被识别为内部或外部命令

mysql - 通过批处理脚本运行mysql脚本

c++ - Private Bytes >> Working Set 是否正常?

windows - 运行 .bat 文件时如何隐藏 ms-dos 窗口?

excel - 使用shell将csv文件保存为excel文件