windows if条件下的批处理文件多个操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12321822/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:51:41  来源:igfitidea点击:

batch file multiple actions under a if condition

windowscommand-line

提问by 5YrsLaterDBA

Is there a way to put multiple actions under a if condition? Like this:

有没有办法在 if 条件下放置多个动作?像这样:

if not exist MyFolderName (
ECHO create a folder
mkdir MyFolderName
)

回答by mjgpy3

You can use &to join commands and execute them on the same line.

您可以使用&来连接命令并在同一行上执行它们。

So your syntax should look like:

所以你的语法应该是这样的:

if not exist MyFolderName ECHO "Create a folder" & mkdir MyFolderName

UPDATE

更新

Or you can use labels to jump to a section containing the commands you want to execute, for example:

或者您可以使用标签跳转到包含您要执行的命令的部分,例如:

if not exist MyFolderName GOTO DOFILESTUFF
:AFTER
...
EXIT

:DOFILESTUFF
ECHO "Create a folder"
mkdir MyFolderName
GOTO AFTER

回答by user11804394

'&' operator works perfectly as mjgpy3 explained. Also, We can add more then 2 statement using &. I have checked with 3 ststement and it works.

'&' 运算符与 mjgpy3 解释的一样完美。此外,我们可以使用 & 添加多于 2 个语句。我已经检查了 3 ststement 并且它有效。

回答by Luis Fernando

Yes, there is!

就在这里!

Check the link below, but remember: you must write the code exactly as the example is shown (all the spaces and parentheses).

检查下面的链接,但请记住:您必须完全按照示例所示编写代码(所有空格和括号)。

Can I have an IF block in DOS batch file?

我可以在 DOS 批处理文件中有一个 IF 块吗?

You can nest other if statements inside if statements, as long as you follow the example. And it works without elsetoo.

只要遵循示例,您就可以在 if 语句中嵌套其他 if 语句。它也可以工作else