lundi 26 juin 2017

Does Batch IF statement handles only one action at a time?

I am new to batch scripting but drawing from my experience with Bash trying to make sense of things. I am errorlevel checking and if %ERRORLEVEL% == 0, I wanted it to echo a message and then increment a counter variable, ELSE echo another message and EXIT with an exit code.

Example of what I have done:

@echo off
SET counter=0

dir /a
SET RC=%ERRORLEVEL%
IF %RC% == 0 ECHO [INFO] - Command executed successfully. SET /A counter=counter+1 ELSE ECHO [ERROR] - Something went wrong here EXIT /B 99

The above example did not work as I expected. The IF-ELSE statement is on a single line and the output I got was

Command executed successfully. SET /A counter=counter+1

When I enclosed the echo and set commands in (), it complains that

(set was unexpected at this time

So, how can I ON TRUE echos a message and increment counter by 1; and on false quit the script with a certain %ERRORLEVEL% code?

Aucun commentaire:

Enregistrer un commentaire