batch file multiple actions under a if condition

蓝咒 提交于 2020-07-08 03:58:07

问题


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

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

回答1:


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



回答2:


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




回答3:


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?

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



来源:https://stackoverflow.com/questions/12321822/batch-file-multiple-actions-under-a-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!