ELSE

Else is an option for the IF command

IF [NOT] EXIST filename (command) ELSE (command)

When combining an ELSE statement with parentheses, always put the opening parenthesis on the same line as ELSE.
This is because CMD does a rather primitive one-line-at-a-time parsing of the command.

so

… ) ELSE ( …

will work, but

… )
ELSE ( …

will fail.

If you follow this rule, you can nest mutiple IF commands to in effect provide an ELSEIF.
Add parenthesis where needed:

… ) ELSE ( IF condition ( action_if_True ) )

) ELSE IF condition ( action_if_True )

) ELSE IF condition action_if_True

Examples

Performing conditional copy/delete operations:

IF EXIST license.txt (copy File1.txt File2.txt) ELSE (Del installer.txt)

Here is a script demonstrating a branch based on the first parameter passed (%1)

@Echo Off
IF %1==dev ( Echo Found dev
   ) ELSE ( If %1==tst ( Echo Found tst
   ) ELSE ( If %1==trn ( Echo Found trn))
)

“Confidence comes not from always being right but from not fearing to be wrong” - Peter T. Mcintyre

Related commands:

IF command


 
Copyright © 1999-2021 SS64.com
Some rights reserved