Dennis
Dennis

Reputation: 4117

Perform action depending on variable value in Batch Files

As the title suggest, how exactly would you do two different action (like below) depending on the value of a variable in a Batch file.

E.g.

IF %NUMBER% = 2 do ECHO Number 2
IF %NUMBER% = 1 do ECHO Number 1

Upvotes: 3

Views: 20240

Answers (2)

dolphy
dolphy

Reputation: 6498

Microsoft has encountered this before. Basically, to fit your scenario:

if %NUMBER% EQU 1 goto number1
if %NUMBER% EQU 2 goto number2
:number1
echo Number 1
:number2
echo Number 2

Upvotes: 4

Dukeatcoding
Dukeatcoding

Reputation: 1393

Here are two examples

IF "%COMPUTERNAME%" == "Bastie" GOTO :TRUE
REM Insert Code for false
  GOTO NEXT
:TRUE
REM Insert Code for true
  echo Willkommen Zuhause
  REM Jetzt wird der if Zweig verlassen
  GOTO NEXT

:NEXT
echo.Have a nice Day!
  1. Beispiel

    IF "%COMPUTERNAME%" == "Bastie" (
    echo Willkommen zu Hause!
    ) ELSE (
    echo Du bist auf Computer: %COMPUTERNAME%
    )
    

Upvotes: 6

Related Questions