Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

batch file multiple actions under a if condition

Tags:

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

if not exist MyFolderName (
ECHO create a folder
mkdir MyFolderName
)
like image 389
5YrsLaterDBA Avatar asked Sep 07 '12 16:09

5YrsLaterDBA


People also ask

What does %% do in batch?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

Can a Batch Script file run multiple commands at the same time?

You can use batch scripts to run multiple commands and instructions on your machine simultaneously. Using a batch script, you will be able to execute all your commands one by one automatically.

What does %1 mean in a batch file?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

Can you pass arguments to a batch file?

In the batch script, you can get the value of any argument using a % followed by its numerical position on the command line. The first item passed is always %1 the second item is always %2 and so on. If you require all arguments, then you can simply use %* in a batch script.


1 Answers

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
like image 186
mjgpy3 Avatar answered Oct 14 '22 14:10

mjgpy3