问题
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