开发者

Batch Scripting Adventure

Here is my batch script file. There are 2 scenarios


  • Scenario 1

@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


  • Scenario 2

@echo off
set n开发者_开发问答ame=
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


  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
    


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.


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.


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
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜