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

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

问题描述

我尝试了解批处理文件中单个命令行中的多个命令是如何工作的.

I try to understand how multiple commands in a single command line in a batch file work.

dir & md folder1 & rename folder1 mainfolder

和其他类似命令的情况,但 & 替换为 &&.

And other case with similar commands, but & substituted with &&.

dir && md folder1 && rename folder1 mainfolder

1.这两种情况有什么区别?

我想问的其他事情:

单行batch.bat:

dir & md folder1 & rename folder1 mainfolder

多线batch.bat:

dir
md folder1
rename folder1 mainfolder

2.就批处理文件程序而言,这种单行和多行是否相同?

还有一件事我想知道:

3.如果我从 main.bat 调用其他批处理文件,它们是否独立且同时运行?主批处理文件不等待其他批处理文件中的结束程序?如何做到这一点?

推荐答案

& 在两个命令之间只会导致独立于第一个命令的结果执行这两个命令.& 的command right 在& 的command left 完成后执行,独立于前一个命令的成功或错误,即独立于前一个命令的退出/返回值.

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

&& 导致有条件地执行第二个命令.只有当第一个命令成功时才会执行第二个命令,这意味着以返回码 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
   )
)

if not errorlevel 1 表示之前的命令 not 以退出代码 greater 0 终止.由于命令 dirmd 永远不会以负值退出,只有 0 或更大(几乎所有命令和控制台应用程序)和值 0 是成功的退出代码,这是测试 dirmd 成功执行的正确方法.

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.

关于 errorlevel 的其他有用的 Stack Overflow 主题:

Other helpful Stack Overflow topics about errorlevel:

由于执行顺序的原因,必须小心混合无条件运算符 & 和条件运算符,例如 &&||不一定是命令行上命令的顺序.

Care must be taken on mixing unconditional operator & with conditional operators like && and || because of the execution order is not necessarily the order of the commands on command line.

示例:

dir "C:Users\%UserName%" /AD 2>nul || dir "%UserProfile%" /AD & echo User profile path: "%UserProfile%"

这个命令行被执行为:

dir "C:Users\%UserName%" /AD 2>nul
if errorlevel 1 dir "%UserProfile%" /AD
echo User profile path: "%UserProfile%"

ECHO 命令总是独立于第一个 DIR 的执行结果而执行,而第二个 DIR 仅在第一个 DIR 在 Windows XP 上失败,或者用户的配置文件文件夹不在驱动器 C: 上,或者根本不在文件夹 Users 中.

The ECHO command is always executed independent on result of execution of first DIR whereas second DIR is executed only if first DIR fails like on Windows XP or the user's profile folder is not on drive C: or not in a folder Users at all.

只有在第一个DIR在第二个DIR 独立于第二个 DIR 的结果.

It is necessary to use ( and ) on executing ECHO only if first DIR fails after second DIR independent on result of second DIR.

dir "C:Users\%UserName%" /AD 2>nul || ( dir "%UserProfile%" /AD & echo User profile path: "%UserProfile%" )

这个命令行被执行为:

dir "C:Users\%UserName%" /AD 2>nul
if errorlevel 1 (
    dir "%UserProfile%" /AD
    echo User profile path: "%UserProfile%"
)

有关第三个问题的答案,请参阅我在如何调用当前批处理文件的父文件夹中的批处理文件的回答? 在那里我解释了使用命令 call 或命令 start 或不使用批处理文件中的这两个命令中的任何一个来运行批处理文件的区别.

For the answer on 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 command start or with none of those two commands from within a batch file.

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

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