使用 Windows 批处理文件的单行多命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25343351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Single line with multiple commands using Windows batch file
提问by Quanti Monati
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. What is the difference between this two cases?
1. 这两种情况有什么区别?
Other thing I want to ask:
还有一点我想问:
One-liner batch.bat
:
单线batch.bat
:
dir & md folder1 & rename folder1 mainfolder
Multi-liner batch.bat
:
多班轮batch.bat
:
dir
md folder1
rename folder1 mainfolder
2. Are this one-liner and multi-liner equal in terms of batch file procedure?
2. 这种单线和多线在批处理文件程序方面是否相同?
And one more thing I would like to know:
还有一件事我想知道:
3. If I call other batch files from a main.bat, are they run independent and simultaneously? Main batch file does not wait for ending procedures in other batch files? How to do that?
3. 如果我从 main.bat 中调用其他批处理文件,它们是否独立并同时运行?主批处理文件不等待其他批处理文件中的结束程序?怎么做?
回答by Mofi
&
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.
&
在两个命令之间只会导致独立于第一个命令的结果执行两个命令。右边的&
命令在左边的命令&
完成后执行,独立于上一个命令的成功或错误,即独立于上一个命令的退出/返回值。
&&
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.
&&
导致有条件地执行第二个命令。只有当第一个命令成功时才执行第二个命令,这意味着退出并返回代码 0。
For an alternate explanation see Conditional Execution.
有关替代解释,请参阅条件执行。
dir & md folder1 & rename folder1 mainfolder
is therefore equal
因此是相等的
dir
md folder1
rename folder1 mainfolder
A multiline replacement for
多行替换
dir && md folder1 && rename folder1 mainfolder
would be
将是
dir
if not errorlevel 1 (
md folder1
if not errorlevel 1 (
rename folder1 mainfolder
)
)
if not errorlevel 1
means the command before did notterminate 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 0is 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.
if not errorlevel 1
指命令之前并没有与退出代码终止0大。由于命令dir
和md
永远不会以负值退出,只有 0 或更大(几乎所有命令和控制台应用程序)和值0是成功的退出代码,这是测试成功执行dir
和的正确方法md
。请参阅 Microsoft 支持文章测试批处理文件中的特定错误级别。
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?
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.
必须小心混合无条件运算符&
和条件运算符&&
,||
因为执行顺序不一定是命令行上命令的顺序。
Example:
例子:
dir "C:\Users\%UserName%" /AD 2>nul || dir "%UserProfile%" /AD & echo User profile path: "%UserProfile%"
This command line is executed as:
此命令行执行为:
dir "C:\Users\%UserName%" /AD 2>nul
if errorlevel 1 dir "%UserProfile%" /AD
echo User profile path: "%UserProfile%"
The ECHOcommand is always executed independent on result of execution of first DIRwhereas second DIRis executed only if first DIRfails like on Windows XP or the user's profile folder is not on drive C: or not in a folder Users
at all.
在ECHO指令总是执行独立于第一执行的结果DIR而第二DIR如果第一只执行DIR或不是一个文件夹中的:失败就像在Windows XP或用户的配置文件夹不上驱动器CUsers
的。
It is necessary to use (
and )
on executing ECHOonly if first DIRfails after second DIRindependent on result of second DIR.
仅当第一个DIR失败后第二个DIR独立于第二个DIR 的结果时,才需要使用(
和)
执行ECHO。
dir "C:\Users\%UserName%" /AD 2>nul || ( dir "%UserProfile%" /AD & echo User profile path: "%UserProfile%" )
This command line is executed as:
此命令行执行为:
dir "C:\Users\%UserName%" /AD 2>nul
if errorlevel 1 (
dir "%UserProfile%" /AD
echo User profile path: "%UserProfile%"
)
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 start
or with none of those two commands from within a batch file.
有关第三个问题的答案,请参阅我关于如何在当前批处理文件的父文件夹中调用批处理文件的回答?在那里我解释了使用命令运行批处理文件call
或使用start
或不使用批处理文件中的这两个命令中的任何一个的差异。