Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if/then/else statements in Windows batch

In a shell script I have the following code:

if echo Mr.32 ; then
  echo Success
else
  echo Failed
  exit
fi

What is the equivalent syntax for Windows batch files?

like image 701
Jeegar Patel Avatar asked Aug 07 '12 11:08

Jeegar Patel


People also ask

Can you use if statements in batch files?

One of the common uses for the 'if' statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the 'if' statement can be done for both strings and numbers.

Is there an else if in batch?

However, you can't use else if in batch scripting. Instead, simply add a series of if statements: if %x%==5 if %y%==5 (echo "Both x and y equal 5.") if %x%==10 if %y%==10 (echo "Both x and y equal 10.") else (echo "Invalid input.")

What is == in batch file?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.

What does %% do in batch file?

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.


2 Answers

I'm having a hard time envisioning when ECHO would fail with a returned ERRORLEVEL not equal 0. I suppose it could fail if the output has been redirected to a file and the target drive is full.

CptHammer has posted a good solution using ERRORLEVEL, although it uses GOTO unnecessarily. It can be done without GOTO using:

ECHO Mr.32
if errorlevel 1 (
  echo Failed
  exit /b
) else (
  echo Success
)

There is a simpler way to take action on SUCCESS or FAILURE of any command.

command && success action || failure action

In your case

ECHO Mr.32&& (
  echo Success
) || (
  echo Failed
  exit /b
)
like image 152
dbenham Avatar answered Sep 23 '22 18:09

dbenham


I think something like this might do the trick:

REM run the command
ECHO Mr.32
IF ERRORLEVEL 1 GOTO failLabel

:successLabel
REM put code to execute in case of success here.
ECHO Success
GOTO endLabel

:failLabel
REM put code here that should be executed in case of failure.
ECHO Failed

:endLabel

This assumes the command you want to test (here: echo MR.32) returns 0 on success and anything higher on failure (BEWARE : Echo in most windows OS will return nothing and therefore, the actual value that is tested in this script is probably the return code from the last command executed just before the script. you're probably better of testing with the command : " DIR someFile.txt" that will return 0 if somefile.txt exists and 1 otherwise.)

It is true as dbenham pointed out, that this structure uses lot of GOTO. This is because this GOTO structure is the only one that will be understood fine in all windows versions. More compact versions appeared with time but they will only work on recent windows versions.

like image 39
cptHammer Avatar answered Sep 19 '22 18:09

cptHammer