Adventure Script Pack

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 enter abcd as input, I get the 'else' is not recognized as an internal or external command, operating program or batch file

Invalid name


  • Scenario 2

 @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 input, I get EQA at this time unexpectedly.

Is there something wrong in my script? I missed something here

+4
source share
4 answers

To put an end to this post, I got the expected result this way -

 @echo off set name= set /P TypeName=Name: %=% if "%TypeName%" == "abcd" ( echo Correct Name ) else ( echo Wrong Name ) 
0
source
  • ELSE must be on the same line as the IF keyword or on the same line with the closing bracket that refers to IF .

    Like this:

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

    Or like this:

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

     IF %TypeName% EQU "abcd" GOTO correctName 
+1
source

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

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

So you are using this format and it should work.

0
source

You don't have to use 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% TypeName% == "abcd", it will go to: correctName if it will not just drop to the next line and go to: wrongName.

0
source

All Articles