加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-13 20:38:01 所属栏目:Windows 来源:网络整理
导读:我知道如果您有两个.bat或.cmd文件,我们称之为foo和bar,以下规则适用: 没有电话: :: Welcome to foo.bat@bar.bat@echo We never get to this line because bar.bat is "chain-executed". 通过电话: :: Welcome to foo.bat@call bar.bat@echo This line is
我知道如果您有两个.bat或.cmd文件,我们称之为foo和bar,以下规则适用:

没有电话:

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

通过电话:

:: 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.

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

必须使用命令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,标题为窗口标题,如果可执行文件是在这种情况下打开的新控制台窗口的控制台应用程序.

然后在启动完成后退出/ 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.

在此批处理代码中,命令exit没有选项/ B,这导致在单独进程中启动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:
转到:如果命令是批处理子程序的一部分,EOF和exit / B都会退出子程序,即使用call:label调用的标签下面的代码,因为批处理子程序就像嵌入在main中的子批处理文件一样关于批处理的批处理文件.

更多示例演示了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 %sn",argv[0],argv[1]);
    }
    else
    {
        printf("Running %s without an argumentn",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行丢失了,因为命令处理器从执行带有参数6的Test.exe直接返回到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,则命令处理器将第二批文件中的退出解释为在第一批文件的上下文中退出.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读