0

I'm starting with batch and I need to do a simple exercise, but I have a few problems.

In the exercise I need to introduce 3 parameters with cmd and check that there are only 3, no more, no less. Then I need to check each parameter to see if everyone equals a letter, (%1 should be a, %2 b and %3 c).

@echo off


if (%1=="") (goto end) else (goto c1)
:c1
if ("%2" =="") (goto error) else (goto c2)
:c2
if ("%3" =="") (goto error) else (goto c3)
:c3
if (%4=="") (goto c4) else (goto c4)
:c4

if ("%1"=="a") (goto p1) else (goto conditions) 
:p1
if ("%2"=="b") (goto p2) else (goto conditions) 
:p2
if ("%3"=="C") (goto end) else (goto conditions) 
goto end


:error
echo "Error"
goto fin


:conditions
echo "Error with parameters"

:end
Compo
  • 36,585
  • 5
  • 27
  • 39

1 Answers1

2

Rather use:

if "%~1" == "" (
 goto end
) else (
  goto c1
)

Brackets are not part of the comparison part of the if syntax. And while they will not produce a syntax error the logic in your conditions is wrong as they will be taken as a part of the compared strings.

Use %~1 to remove the quotes around the argument (if there are such) and put quotes to handle the spaces in the arguments.

Put spaces before and after brackets as in some cases they could to syntax error if not separated from the commands. I personally prefer new lines as they improve readability.

Better start from checking the last argument (4 in your case) as if one is omitted the cmd parser will start counting from 1.

npocmaka
  • 55,367
  • 18
  • 148
  • 187