5YrsLaterDBA
5YrsLaterDBA

Reputation: 34790

batch file multiple actions under a if condition

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

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

Upvotes: 27

Views: 80252

Answers (3)

Luis Fernando
Luis Fernando

Reputation: 25

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.

Upvotes: 0

user11804394
user11804394

Reputation: 1

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

Upvotes: 0

mjgpy3
mjgpy3

Reputation: 8947

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

Upvotes: 41

Related Questions