使用Windows批处理文件多个命令单线 [英] Single line with multiple commands using Windows batch file

查看:229
本文介绍了使用Windows批处理文件多个命令单线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去了解如何在批处理文件做作品多个命令。

I try to understand how does multiple commands in batch file works.

dir & md folder1 & rename folder1 mainfolder

和其他情况类似的命令,但和放大器;与&功放取代;&安培;

and other case with similar commands, but & substituted with &&

dir && md folder1 && rename folder1 mainfolder

1。是什么这两种情况之间的区别?

1. What is the difference between this two cases?

*********另一件事我要问*********

********* OTHER THING I WANT TO ASK *********

Oneliner batch.bat:

Oneliner batch.bat:

dir & md folder1 & rename folder1 mainfolder

多元线性batch.bat:

Multiple liner batch.bat:

dir
md folder1
rename folder1 mainfolder

2。在批处理文件程序?

2. Are this one-liner and multiple-liner equal in terms of batch file procedure?

*******还有一件事*******

******* AND ONE MORE THING *******

3。如果我将从main.bat调用其他批处理文件,他们将独立运行,同时?主要批处理文件将不会等待结束其他批处理文件的程序?那怎么办?

3. If I will call other batch files from the main.bat, will they run independent and simultaneously? Main batch file will not wait for ending procedures in other batch files? How to do that?

推荐答案

&安培; 两个命令之间只是导致执行命令都独立于第一个命令的结果。指挥权&安培; 的命令左侧后执行&安培; 完成独立于$ P的成功或错误$ pvious命令,即独立于previous命令的退出/返回值。

& between two commands simply results in executing both commands independent on result of first command. The command right of & is executed after command left of & finished independent on success or error of the previous command, i.e. independent on exit / return value of previous command.

&放大器;&安培; 结果在第二个命令的条件执行。第二个命令仅当第一个命令是成功的,这意味着,返回code 0退出执行。

&& results in a conditional execution of second command. The second command is executed only if first command was successful which means exited with return code 0.

有关备用说明,请参见条件执行

For an alternate explanation see Conditional Execution.

dir & md folder1 & rename folder1 mainfolder

因此​​等于

dir
md folder1
rename folder1 mainfolder

一个多行替换

dir && md folder1 && rename folder1 mainfolder

dir
if not errorlevel 1 (
   md folder1
   if not errorlevel 1 (
      rename folder1 mainfolder
   )
)

如果没有ERRORLEVEL 1 表示命令之前做过的不可以以退出code终止大于0 。由于命令 DIR MD 从不退出了负值,只是0或更大(如几乎所有的命令和控制台应用程序)和值 0 是退出code的成功,这是测试上的成功执行 DIR 和正确的方法 MD 。请参阅Microsoft支持文章测试在批处理文件一个特定的错误级别。

if not errorlevel 1 means the command before did not terminate with an exit code greater 0. As the commands dir and md never exit with a negative value, just with 0 or greater (as nearly all commands and console applications) and value 0 is the exit code for success, this is a correct method to test on successful execution of dir and md. See the Microsoft support article Testing for a Specific Error Level in Batch Files.

有关errorlevel其他有用的堆栈溢出主题:

Other helpful Stack Overflow topics about errorlevel:

  • Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
  • What are the ERRORLEVEL values set by internal cmd.exe commands?

有关第三个问题的答案看我的答案上如何调用批处理文件在当前批处理文件的父文件夹?我在那里上运行时命令的批处理文件,解释差异呼叫启动或没有的从批处理文件中的两个命令。

For the answer on your third question see my answer on How to call a batch file in the parent folder of current batch file? where I have explained the differences on running a batch file with command call or with start or with none of those 2 commands from within a batch file.

这篇关于使用Windows批处理文件多个命令单线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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