0

I need to check if one or two parameters are given by the user. Depending on that, different functions will apply.

How can i use anything like OR or AND in this case?

  @echo off
    :BEGIN
            echo START
    
            set /p var1=
            set /p var2=
    
            if %var1%=="" if %var2%=="" goto BEGIN
            if exist %var1% OR %var2% goto ONE
            if exist %var1% if exist %var2% goto TWO
    
    :ONE
            ECHO You give only one parameter
            <code here>
            goto EXIT
    
    :TWO
            ECHO You give two parameters
            <code here>
            goto EXIT
    
    
    :EXIT
    exit

Thanks!

Altomic
  • 11
  • 3

1 Answers1

2

Batch simply doesn't have logical operators, but you nearly get a solution.

In batch, string compares are a bit different than in other languages.

The variable has to be enclosed in quotes, too. if "%var1%"==""

Then your code looks like

@echo off
:BEGIN
echo START

set "var1="
set "var2="
set /p var1=
set /p var2=

if "%var1%"=="" if "%var2%"=="" goto BEGIN
if "%var1%" NEQ "" if "%var2%" NEQ "" goto TWO
goto :ONE

:ONE
ECHO You give only one parameter
<code here>
goto EXIT

:TWO
ECHO You give two parameters
<code here>
goto EXIT


:EXIT
exit

Instead of using a string compare you could also use the IF DEFINED syntax

if not defined var1 if not defined var2 goto begin
if defined var1 if defined var2 goto TWO

Btw. I added the set "var1=" to undefine the variable before the set /p, because a set /p will not change (or empty) a variable, for an empty input.

jeb
  • 78,592
  • 17
  • 171
  • 225