在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 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: 笔记2: 更多示例演示了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,则命令处理器将第二批文件中的退出解释为在第一批文件的上下文中退出. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在哪里可以找到Microsoft .NET Framework开发指南?
- 防病毒 – Windows Defender和Microsoft Windows恶意软件删
- windows sc 添加创建exe服务;
- active-directory – 如何防止用户将XP机器插入网络
- 从Windows文件夹到Linux分区
- windows上安装python虚拟环境
- windows-server-2008 – 域控制器上的HyperV还是HyperV上的
- windows-server-2003 – 如何获取DNS服务器统计信息?每日总
- windows – 如何在每天的某个时间自动打开网站
- Xamarin 学习笔记 - 配置环境(Windows & iOS)
- windows-mobile – Windows Mobile设备有什么好的
- 如何每隔1秒更改Windows批处理脚本的cmd文本颜色
- datatemplate – Metro风格应用程序中的XAML图像
- Notepad++ 安装NppFtp,方便在Windows上远程打开
- 在Powerershell for Windows中使用Start-Job调用
- .net – 如何引用DLL,无论版本如何?
- windows – AutoHotKey:带多行输入的InputBox
- filesystems – 没有为NTFS使用512字节集群的任何
- xaml – 在Windows Phone上设置ListView的GroupS
- winapi – 从内核模式关闭Windows?