我了解您是否有两个.bat
或.cmd
文件,我们将它们称为foo
和bar
,适用以下规则:
没有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 :EOF
和exit /B
都将导致退出子例程,即该命令位于名为call :label
的标签下的代码,因为批处理子例程就像嵌入在有关批处理的主批处理文件中的子批处理文件一样。一些其他示例来演示
call
和exit /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
是具有文件扩展名bat
或cmd
的批处理文件,或者是具有文件扩展名exe
或com
的可执行文件。这可以通过将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/