hari
hari

Reputation: 1377

Batch Scripting Adventure

Here is my batch script file. There are 2 scenarios



@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% == "abcd" goto correctName
else goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

When i type abcd as the input, i get 'else' is not recognized as an internal or external command,operable program or batch file

Wrong Name



@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% EQA "abcd" goto correctName
if %TypeName% NEQ "abcd" goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

When i type abcd as the input, i get EQA was unexpected at this time.

Is there something wrong in my script?Am I missing something here

Upvotes: 4

Views: 190

Answers (4)

hari
hari

Reputation: 1377

To give an end to this post,i got the expected output this way-

@echo off
set name=
set /P TypeName=Name: %=%

if "%TypeName%" == "abcd" (
echo Correct Name
) else (
echo Wrong Name
)

Upvotes: 0

Andriy M
Andriy M

Reputation: 77687

  1. ELSE should be on the same line with the IF keyword or on the same line with the closing bracket that pertains to the IF.

    Like this:

    IF %TypeName% == "abcd" GOTO correctName ELSE GOTO wrongName
    

    Or like this:

    IF %TypeName% == "abcd" (
      ECHO Correct.
      GOTO correctName
    ) ELSE GOTO wrongName
    
  2. The correct keyword for the Equal operator is EQU:

    IF %TypeName% EQU "abcd" GOTO correctName
    

Upvotes: 1

Bali C
Bali C

Reputation: 31231

You shouldn't necessarily need to use the else, like this

@echo off
set name=
set /P TypeName=Name: %=%

if %TypeName% == "abcd" goto correctName
goto wrongName

:correctName
echo Correct Name
:end

:wrongName
echo Wrong Name
:end

If the %TypeName% == "abcd" it will jump to :correctName, if it doesn't it will simply fall to the next line and jump to :wrongName.

Upvotes: 0

laurent
laurent

Reputation: 90776

The first example is almost right, except that the format of an IF/ELSE statement in a batch file is as follow:

IF <statement>  (
..
..
) ELSE (
...
...
)

So just you use that format and it should work.

Upvotes: 0

Related Questions