Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 345
Dennis Avatar asked Dec 30 '25 02:12

Dennis


2 Answers

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%
    )
    
like image 191
Dukeatcoding Avatar answered Dec 31 '25 18:12

Dukeatcoding


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
like image 23
dolphy Avatar answered Dec 31 '25 19:12

dolphy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!