如何在.BAT文件中运行多个.BAT文件

时间:2022-11-14 16:03:40

I'm trying to get my commit-build.bat to execute other .BAT files as part of our build process.

我正在努力实现我的承诺。bat作为构建过程的一部分执行其他. bat文件。

Content of commit-build.bat:

commit-build.bat内容:

"msbuild.bat"
"unit-tests.bat"
"deploy.bat"

This seems simple enough, but commit-build.bat only executes the first item in the list (msbuild.bat).

这看起来很简单,但是提交-构建。bat只执行列表中的第一个项目(msbuild.bat)。

I have run each of the files separately with no problems.

我已经分别运行了每个文件,没有问题。

13 个解决方案

#1


927  

use

使用

call msbuild.bat
call unit-tests.bat
call deploy.bat

When not using CALL, the current batch file stops and the called batch file starts executing. It's a peculiar behavior dating back to the early MS-DOS days

当不使用调用时,当前批处理文件停止,被调用的批处理文件开始执行。这是一种特殊的行为,可以追溯到MS-DOS时代。

#2


151  

All the other answers are correct: use call. for example:

其他答案都是正确的:使用call。例如:

 call "msbuild.bat"

History

历史

In ancient dos versions it was not possible to recursively execute batch files. Then the call command was introduced that called another cmd shell to execute the batch file and returned execution back to the calling cmd shell when finished.

在古老的dos版本中,不可能递归地执行批处理文件。然后引入了调用命令,该命令调用另一个cmd shell来执行批处理文件,并在完成时返回调用cmd shell的执行。

Obviously in later versions no other cmd shell was necessary anymore.

显然在以后的版本中不再需要其他cmd shell。

In the early days many batch files depended on the fact that calling a batch file would not return to the calling batch file. Changing that behaviour without additional syntax would have broken many systems like batch menu systems (using batch files for menu structures).

在早期,许多批处理文件依赖于调用批处理文件不会返回到调用批处理文件这一事实。改变这种行为而不使用其他语法将会破坏许多系统,如批处理菜单系统(使用批处理文件作为菜单结构)。

As in many cases with Microsoft, backward compatibility therefore is the reason for this behaviour.

与微软的许多情况一样,向后兼容是这种行为的原因。

Tips

提示

If your batch files have spaces in their names, use quotes around the name:

如果您的批处理文件的名称中有空格,请在名称中使用引号:

call "unit tests.bat"

By the way: if you do not have all the names of the batch files, you could also use for to do this: (does not guarantee the correct order of batch file calls, follows order of file system)

顺便说一句:如果您没有批处理文件的所有名称,您还可以使用:(不保证批处理文件调用的顺序正确,按照文件系统的顺序)

FOR %x IN (*.bat) DO call "%x"

You can also react on errorlevels after a call. Use

您还可以在调用后对错误级别作出反应。使用

exit /B 1   # or any other integer value in 0..255

to give back an errorlevel. 0 denotes correct execution. In the calling batch file you can react using

返回错误级别。0表示正确执行。在调用批处理文件中,您可以使用

if errorlevel neq 0 <batch command>

Use if errorlevel 1 if you have a an older Windows then NT4/2000/XP to catch all errorlevels 1 and greater.

如果您有一个较老的Windows,那么使用if errorlevel 1,然后使用NT4/2000/XP来捕获所有的errorlevel 1或更高。

To control the flow of a batch file, there is goto :-(

要控制批处理文件的流,可以使用goto:-(

if errorlevel 2 goto label2
if errorlevel 1 goto label1
...
:label1
...
:label2
...

As others pointed out: have a look at build systems to replace batch files.

正如其他人指出的:查看构建系统以替换批处理文件。

#3


84  

If we want to open multiple command prompt then we could use the

如果我们想打开多个命令提示符,那么我们可以使用

start cmd /k 

/k: is compulsory which will execute.

k:强制执行。

launching many command propmts can be done as below.

启动多个命令进程可以如下所示。

start cmd /k Call rc_hub.bat 4444

start cmd /k Call rc_grid1.bat 5555

start cmd /k Call rc_grid1.bat 6666

start cmd /k Call rc_grid1.bat 5570.

#4


35  

try

试一试

call msbuild.bat
call unit-tests.bat
call deploy.bat

#5


22  

You are calling multiple batches in an effort to compile a program. I take for granted that if an error occurs:
1) The program within the batch will exit with an errorlevel;
2) You want to know about it.

为了编译一个程序,您正在调用多个批。我想当然地认为,如果发生错误:1)批内的程序将以错误级别退出;你想知道这件事。

for %%b in ("msbuild.bat" "unit-tests.bat" "deploy.bat") do call %%b|| exit /b 1

'||' tests for an errorlevel higher than 0. This way all batches are called in order but will stop at any error, leaving the screen as it is for you to see any error message.

||对大于0的错误级别的测试。这样,所有批次都按顺序调用,但在出现任何错误时将停止,使您可以看到任何错误消息。

#6


18  

call msbuild.bat
call unit-tests.bat
call deploy.bat

#7


17  

To call a .bat file within a .bat file, use

要在.bat文件中调用.bat文件,请使用

call foo.bat

(Yes, this is silly, it would make more sense if you could call it with foo.bat, like you could from the command prompt, but the correct way is to use call.)

(是的,这很愚蠢,如果你可以用foo调用它的话会更有意义。bat,就像从命令提示符中可以看到的那样,但是正确的方法是使用call。

#8


12  

If we have 2 batch script aaa.bat & bbb.bat and call like below

如果我们有两个批处理脚本aaa.bat和bbb。像下面一样敲打和呼叫

call aaa.bat
call bbb.bat

When execute the script, It will call aaa.bat first and wait for the thread of aaa.bat terminate then will call bbb.bat

执行脚本时,它将首先调用aaa.bat,然后等待aaa.bat终止的线程,然后调用bbb.bat

But if you don't want to wait aaa.bat terminate to call bbb.bat, try to use START command

但是如果你不想等aaa.bat终止呼叫bbb。bat,尝试使用START命令。

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
  [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
  [/AFFINITY <hex affinity>] [/WAIT] [/B] [command/program]
  [parameters]

Exam:

考试:

start /b aaa.bat
start /b bbb.bat

#9


6  

looking at your filenames, have you considered using a build tool like nant or ant (the java version). You'll get a lot more control than bat files

查看您的文件名,您是否考虑过使用像nant或ant (java版本)这样的构建工具。你将得到比bat文件更多的控制

#10


4  

Start msbuild.bat
Start unit-tests.bat
Start deploy.bat

If that doesn't work, replace start with call or try this:

如果不行,用“开始”替代“开始”,或者试试这个:

Start msbuild.bat
Goto :1
:1
Start unit-tests.bat
Goto :2
:2
Start deploy.bat

#11


2  

Running multiple scripts in one I had the same issue. I kept having it die on the first one not realizing that it was exiting on the first script.

在一个脚本中运行多个脚本,我遇到了同样的问题。我一直让它在第一个脚本上死去,却没有意识到它在第一个脚本上的存在。

:: OneScriptToRunThemAll.bat
CALL ScriptA.bat
CALL ScriptB.bat
EXIT

:: ScriptA.bat
Do Foo
EXIT
::ScriptB.bat
Do bar
EXIT

I removed all 11 of my scripts EXIT lines and tried again and all 11 ran in order one at a time in the same command window.

我删除了所有的11个脚本退出行,然后再次尝试,所有11个都在同一个命令窗口中依次运行。

:: OneScriptToRunThemAll.bat
CALL ScriptA.bat
CALL ScriptB.bat
EXIT

::ScriptA.bat
Do Foo

::ScriptB.bat
Do bar

#12


2  

If you want to open many batch files at once you can use the call command however the call command closes the current bat file and goes to another if you want to open many at once you may want to try this

如果想同时打开多个批处理文件,可以使用call命令,但是call命令会关闭当前的bat文件,如果想要同时打开多个批处理文件,可以使用另一个

@echo off
start cmd "call ex1.bat&ex2.bat&ex3.bat" 

and so on or repeat start cmd "call..." for however many files. this works for WIN 7 not sure about other systems.

诸如此类或重复启动cmd的“调用……”,不管有多少文件。这适用于win7不确定的其他系统。

#13


0  

Just use the call command! Here is an example:

只需使用呼叫命令!这是一个例子:

call msbuild.bat
call unit-tests.bat
call deploy.bat

Cheers!

干杯!

#1


927  

use

使用

call msbuild.bat
call unit-tests.bat
call deploy.bat

When not using CALL, the current batch file stops and the called batch file starts executing. It's a peculiar behavior dating back to the early MS-DOS days

当不使用调用时,当前批处理文件停止,被调用的批处理文件开始执行。这是一种特殊的行为,可以追溯到MS-DOS时代。

#2


151  

All the other answers are correct: use call. for example:

其他答案都是正确的:使用call。例如:

 call "msbuild.bat"

History

历史

In ancient dos versions it was not possible to recursively execute batch files. Then the call command was introduced that called another cmd shell to execute the batch file and returned execution back to the calling cmd shell when finished.

在古老的dos版本中,不可能递归地执行批处理文件。然后引入了调用命令,该命令调用另一个cmd shell来执行批处理文件,并在完成时返回调用cmd shell的执行。

Obviously in later versions no other cmd shell was necessary anymore.

显然在以后的版本中不再需要其他cmd shell。

In the early days many batch files depended on the fact that calling a batch file would not return to the calling batch file. Changing that behaviour without additional syntax would have broken many systems like batch menu systems (using batch files for menu structures).

在早期,许多批处理文件依赖于调用批处理文件不会返回到调用批处理文件这一事实。改变这种行为而不使用其他语法将会破坏许多系统,如批处理菜单系统(使用批处理文件作为菜单结构)。

As in many cases with Microsoft, backward compatibility therefore is the reason for this behaviour.

与微软的许多情况一样,向后兼容是这种行为的原因。

Tips

提示

If your batch files have spaces in their names, use quotes around the name:

如果您的批处理文件的名称中有空格,请在名称中使用引号:

call "unit tests.bat"

By the way: if you do not have all the names of the batch files, you could also use for to do this: (does not guarantee the correct order of batch file calls, follows order of file system)

顺便说一句:如果您没有批处理文件的所有名称,您还可以使用:(不保证批处理文件调用的顺序正确,按照文件系统的顺序)

FOR %x IN (*.bat) DO call "%x"

You can also react on errorlevels after a call. Use

您还可以在调用后对错误级别作出反应。使用

exit /B 1   # or any other integer value in 0..255

to give back an errorlevel. 0 denotes correct execution. In the calling batch file you can react using

返回错误级别。0表示正确执行。在调用批处理文件中,您可以使用

if errorlevel neq 0 <batch command>

Use if errorlevel 1 if you have a an older Windows then NT4/2000/XP to catch all errorlevels 1 and greater.

如果您有一个较老的Windows,那么使用if errorlevel 1,然后使用NT4/2000/XP来捕获所有的errorlevel 1或更高。

To control the flow of a batch file, there is goto :-(

要控制批处理文件的流,可以使用goto:-(

if errorlevel 2 goto label2
if errorlevel 1 goto label1
...
:label1
...
:label2
...

As others pointed out: have a look at build systems to replace batch files.

正如其他人指出的:查看构建系统以替换批处理文件。

#3


84  

If we want to open multiple command prompt then we could use the

如果我们想打开多个命令提示符,那么我们可以使用

start cmd /k 

/k: is compulsory which will execute.

k:强制执行。

launching many command propmts can be done as below.

启动多个命令进程可以如下所示。

start cmd /k Call rc_hub.bat 4444

start cmd /k Call rc_grid1.bat 5555

start cmd /k Call rc_grid1.bat 6666

start cmd /k Call rc_grid1.bat 5570.

#4


35  

try

试一试

call msbuild.bat
call unit-tests.bat
call deploy.bat

#5


22  

You are calling multiple batches in an effort to compile a program. I take for granted that if an error occurs:
1) The program within the batch will exit with an errorlevel;
2) You want to know about it.

为了编译一个程序,您正在调用多个批。我想当然地认为,如果发生错误:1)批内的程序将以错误级别退出;你想知道这件事。

for %%b in ("msbuild.bat" "unit-tests.bat" "deploy.bat") do call %%b|| exit /b 1

'||' tests for an errorlevel higher than 0. This way all batches are called in order but will stop at any error, leaving the screen as it is for you to see any error message.

||对大于0的错误级别的测试。这样,所有批次都按顺序调用,但在出现任何错误时将停止,使您可以看到任何错误消息。

#6


18  

call msbuild.bat
call unit-tests.bat
call deploy.bat

#7


17  

To call a .bat file within a .bat file, use

要在.bat文件中调用.bat文件,请使用

call foo.bat

(Yes, this is silly, it would make more sense if you could call it with foo.bat, like you could from the command prompt, but the correct way is to use call.)

(是的,这很愚蠢,如果你可以用foo调用它的话会更有意义。bat,就像从命令提示符中可以看到的那样,但是正确的方法是使用call。

#8


12  

If we have 2 batch script aaa.bat & bbb.bat and call like below

如果我们有两个批处理脚本aaa.bat和bbb。像下面一样敲打和呼叫

call aaa.bat
call bbb.bat

When execute the script, It will call aaa.bat first and wait for the thread of aaa.bat terminate then will call bbb.bat

执行脚本时,它将首先调用aaa.bat,然后等待aaa.bat终止的线程,然后调用bbb.bat

But if you don't want to wait aaa.bat terminate to call bbb.bat, try to use START command

但是如果你不想等aaa.bat终止呼叫bbb。bat,尝试使用START命令。

START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
  [/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
  [/AFFINITY <hex affinity>] [/WAIT] [/B] [command/program]
  [parameters]

Exam:

考试:

start /b aaa.bat
start /b bbb.bat

#9


6  

looking at your filenames, have you considered using a build tool like nant or ant (the java version). You'll get a lot more control than bat files

查看您的文件名,您是否考虑过使用像nant或ant (java版本)这样的构建工具。你将得到比bat文件更多的控制

#10


4  

Start msbuild.bat
Start unit-tests.bat
Start deploy.bat

If that doesn't work, replace start with call or try this:

如果不行,用“开始”替代“开始”,或者试试这个:

Start msbuild.bat
Goto :1
:1
Start unit-tests.bat
Goto :2
:2
Start deploy.bat

#11


2  

Running multiple scripts in one I had the same issue. I kept having it die on the first one not realizing that it was exiting on the first script.

在一个脚本中运行多个脚本,我遇到了同样的问题。我一直让它在第一个脚本上死去,却没有意识到它在第一个脚本上的存在。

:: OneScriptToRunThemAll.bat
CALL ScriptA.bat
CALL ScriptB.bat
EXIT

:: ScriptA.bat
Do Foo
EXIT
::ScriptB.bat
Do bar
EXIT

I removed all 11 of my scripts EXIT lines and tried again and all 11 ran in order one at a time in the same command window.

我删除了所有的11个脚本退出行,然后再次尝试,所有11个都在同一个命令窗口中依次运行。

:: OneScriptToRunThemAll.bat
CALL ScriptA.bat
CALL ScriptB.bat
EXIT

::ScriptA.bat
Do Foo

::ScriptB.bat
Do bar

#12


2  

If you want to open many batch files at once you can use the call command however the call command closes the current bat file and goes to another if you want to open many at once you may want to try this

如果想同时打开多个批处理文件,可以使用call命令,但是call命令会关闭当前的bat文件,如果想要同时打开多个批处理文件,可以使用另一个

@echo off
start cmd "call ex1.bat&ex2.bat&ex3.bat" 

and so on or repeat start cmd "call..." for however many files. this works for WIN 7 not sure about other systems.

诸如此类或重复启动cmd的“调用……”,不管有多少文件。这适用于win7不确定的其他系统。

#13


0  

Just use the call command! Here is an example:

只需使用呼叫命令!这是一个例子:

call msbuild.bat
call unit-tests.bat
call deploy.bat

Cheers!

干杯!