在Windows批处理文件,你能链执行的东西是*不*另一个批处理文件? [英] In a Windows batch file, can you chain-execute something that is *not* another batch file?

查看:113
本文介绍了在Windows批处理文件,你能链执行的东西是*不*另一个批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,如果你有两个的.bat 的.cmd 文件,我们姑且称之为,以下规则:

I understand if you have two .bat or .cmd files, let's call them foo and bar, the following rules apply:

没有呼叫

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

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

My question is: is there a way of performing the converse operation, i.e. ensuring that a non-batch-file executable is chained?

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

在换句话说,有没有进行虚拟链的功能的一种方式在最后一个例子命令?

In other words, is there a way of performing the function of the fictitious chain command in that last example?

推荐答案

有必要使用命令开始,即可在一个单独的进程中运行的可执行文件和另外的退出当前批量处理,整个过程命令

It is necessary to use command start to run the executable in a separate process and additionally exit current batch processing or entire command process.

@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 在一个单独的进程与标题作为窗口标题中可执行文件的情况下在这种情况下,打开新的控制台窗口的控制台应用程序。

This batch file starts bar.exe in a separate process with Title as window title in case of executable is a console application for the new console window opened in this case.

再经过启动完成退出/ B 无条件通过命令处理器执行的,而酒吧.EXE 是在一个单独的进程中运行导致终止当前的批处理文件的处理。

Then after start finished exit /B is unconditionally executed by command processor while bar.exe is running in a separate process resulting in terminating processing of current batch file.

如果这个批处理文件不是用命令的呼叫从另一个批处理文件名为本身,命令处理器完现加工导致退出命令处理批处理文件,除了批处理文件调用了的cmd.exe 与选项 / K 保持命令提示符窗口中完成批量处理这不是默认的情况下开启后。

If this batch file was not called itself from another batch file using command call, the command processor finishes now processing the batch file resulting in exiting command processing, except the batch file was called with cmd.exe with option /K to keep command prompt window open after finishing batch processing which is not the case by default.

但如果此批处理文件被称为是呼叫从另一个批处理文件,这个孩子批处理文件刚刚处理完毕,并命令处理器继续父批文件的处理,而酒吧.EXE 运行在一个单独的进程。

But if this batch file was called with call from another batch file, just processing of this child batch file is finished and command processor continues processing of parent batch file while bar.exe runs in a separate process.

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

在此批code命令的退出无选项 / B 这之后启动完成启动 bar.exe 在单独的进程,即使当前的批处理文件是从拨打另一个批处理文件即使启动批处理文件处理与的cmd.exe 带参数 / K

In this batch code the command exit is without option /B which results in terminating command processing after start finished starting bar.exe in a separate process even if the current batch file was called from another batch file with call and even if batch file processing was started with cmd.exe with parameter /K.

而不是无条件地串联两个命令的开始退出与运营商&安培; ,也可以使用如下所示的两个变体的块

Instead of unconditionally concatenating the two commands start and exit with operator & it is also possible to use a block as shown below for the two variants.

只需退出当前批处理:

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

通过退出整个命令过程:

With exiting entire command process:

@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 取决于至少一个条件开始当然只能意会,使该批处理文件。

Such a start of an application with exiting either current batch processing or entire command processing makes of course only sense when bar.exe is started depending on at least one condition in the batch file.

注1:结果
也可以使用转到:EOF 而不是退出/ B 来结束当前的批处理

Note 1:
It is also possible to use goto :EOF instead of exit /B to end current batch processing.

注2:
转到:EOF 退出/ B 导致无论是在刚刚离开子程序如果命令是一个批处理子程序是一部分,即code进行调用标签下的呼叫:标签,因为一批子程序就像是对批处理的嵌入式主批处理文件中的一个子批处理文件

Note 2: goto :EOF and exit /B result both in just exiting the subroutine if the command is being part of a batch subroutine, i.e. code below a label called with call :label because a batch subroutine is just like a child batch file embedded within a main batch file regarding batch processing.

更多的例子来证明呼叫的行为和退出/ B

Some more examples to demonstrate the behavior of call and 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 from within a command prompt window results in output:

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

所以行成品Test2.bat 丢失,因为命令处理器从 Test3.bat 直接返回到 Test1.bat

So the line Finished Test2.bat is missing because command processor returned from Test3.bat directly to Test1.bat.

接下来我们编译如下C code到控制台应用程序将Test.exe

Next we compile following C code to console application 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 在以下两个批处理文件:

And we make use of Test.exe in following 2 batch files:

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 from within a command prompt window results in output:

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

所以行成品Test5.bat 丢失,因为从执行将Test.exe 带参数返回命令处理器 6 直接 Test4.bat

So the line Finished Test5.bat is missing because command processor returned from executing Test.exe with argument 6 directly to Test4.bat.

不过,使用酒吧和放大器;出口/ B 但它仍然是重要的,如果是具有文件扩展名的批处理文件蝙蝠 CMD ,或与文件扩展名 exe文件可执行文件或 COM 。这可以通过更改 Test2.bat 的code加以论证:

But with using bar & exit /B it is nevertheless important if bar is a batch file with file extension bat or cmd, or an executable with file extension exe or com. This can be demonstrated by changing code of Test2.bat to:

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

运行 Test1.bat 从命令提示符窗口中的结果输出:

Running Test1.bat from within a command prompt window results in output:

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

因此​​,与退出/ B 追加的第二批文件,命令处理器间$ P $点的退出在第二批文件作为背景下退出第一批文件。

So with exit /B appended in second batch file, command processor interprets exit in second batch file as exit in context of first batch file.

这篇关于在Windows批处理文件,你能链执行的东西是*不*另一个批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆