Jump to content

Else statements for MS-DOS?


Recommended Posts

By reference the input is save it as a %Something%

Here's the entire code, It's a bit nooby seeing as im just testing things and messing around,

Here is a test example of using Else. Tested with Windows XP.


@echo off
title Hello
color b

REM Greeting code section

echo Script: Hello
set /p Hello="User: "

if "%Hello%"=="Hello" (
call :sub_2 %Hello%
) else if "%Hello%"=="Hi" (
call :sub_2 %Hello%
) else (
set Hello=Hello
echo Script: Incorrect greeting received.
)

set Hello=

REM Feeling code section

echo Script: How are you feeling?
set /p Hello="User: "

if "%Hello%"=="Fine" (
call :sub_4 %Hello%
) else if "%Hello%"=="Good" (
call :sub_4 %Hello%
) else if "%Hello%"=="Great" (
call :sub_4 %Hello%
) else if "%Hello%"=="Cool" (
call :sub_4 %Hello%
) else if "%Hello%"=="Bad" (
call :sub_4 %Hello%
) else if "%Hello%"=="Cold" (
call :sub_4 %Hello%
) else if "%Hello%"=="Hot" (
call :sub_4 %Hello%
) else if "%Hello%"=="Weird" (
call :sub_4 %Hello%
) else if "%Hello%"=="I dunno you tell me" (
call :sub_4 %Hello%
) else (
echo Script: Feeling is not registered.
)

set Hello=

REM User name code section

echo Script: What is your name?
set /p Hello="User: "

if "%Hello%"=="" (
echo Script: Invalid Name!!!
) else (
call :sub_5 %Hello%
)

set Hello=

pause
exit

REM Subroutine code section

:sub_2
echo Script: Thankyou for typing %1
goto :eof

:sub_4
echo Script: Oh so you're feeling %Hello%.
goto :eof

:sub_5
title Hello %1!
echo Script: Hi, %1! My name is Command Prompt
pause
echo.
echo Script: Bye %1!
goto :eof

Note:

Excess echos removed for this test.

Using Call (using labels) as a subroutine execution then returning to called line is IMO easier to trace then using Goto a label.

Ask if you need help understanding any part of the test script though it would be nice for you to try and figure it out yourself.

Link to comment
Share on other sites


I would go about this a different way, since you can not possibly account for every name that someone would enter. You have already been given a suggestion for using a "pick-list" for the users to choose from, mine will handle only the names that you provide a subroutine process to account for that particular name. I have also changed the variable from"hello" to "name" (makes things easier to follow).

@echo off

:top
set /p %name%=Enter Name:

echo Hello, %name%

call :%name% 2>nul

if errorlevel 1 (
echo Bad name
goto top)

goto :eof

:: Provide a subroutine for each user name below
:bob
echo Running specific commands for Bob...
goto :eof

:john
echo Running specific commands for John...
goto :eof

To carry this one step further, to retrieve the user's feeling:

@echo off

:top
set /p name=Enter Name:

echo Hello, %name%

call :%name% 2>nul

if errorlevel 1 (
echo Bad name
goto top)

:getfeeling
set /p Feeling=How are you feeling %name% (Fine, Good, Great)?

echo %name% is feeling %feeling%...

call :_%feeling% 2>nul

if errorlevel 1 (
echo Bad feeling...
goto getfeeling)

goto :eof

:: Provide a subroutine for each user name below
:bob
echo Running specific commands for Bob...
goto :eof

:john
echo Running specific commands for John...
goto :eof


:: Provide a subroutine for each user feeling below (start each with an underscore "_")
:_fine
echo Running commands for users that are feeling Fine...
goto :eof

:_good
echo Running commands for users that are feeling Good...
goto :eof

:_great
echo Running commands for users that are feeling Great...
goto :eof

I tried to keep it simple. You will still need to add the actions for each user and feeling.

Also, it does not take into account if the user enters his/her full name.

i.e. if the user enters Bob Smith, the script will still run for user Bob.

HTH

Link to comment
Share on other sites

The only other thing to do is bite the bullet and accept that if someone wants to be called frog it isn't going to kill you or the script so don't bother checking it.

You could then use the same structure you already have, with a few small tweaks.

The improvement I'd suggest then is to extend your accepted greetings/feelings input using findstr.

@Echo off&Setlocal
Title Hello :P
Color b
Set "Ecko=Echo/&Echo/"
:1
Set/p "Greet=Hello "
%Ecko%
Echo %Greet%|Findstr/ir "Hello \<Hi\> Bonjour Howdy Gday Ola\>">Nul||(%Ecko%
Echo Incorrect greeting
%Ecko%
Goto 1)
:2
%Ecko%
Set/p "Feel=How are you? "
Echo %Feel%|Findstr/i "Fine Good Great Cool Bad Cold Hot Weird Dunno">Nul||(
%Ecko%
Echo Feeling is not registered.
%Ecko%
Goto 2)
%Ecko%
Echo Oh so you're feeling %Feel%!
%Ecko%
Echo Just wondering...
:3
%Ecko%
Set/p "Nick=What's your name? "
If /i Not "%Nick%"=="" Goto 4
%Ecko%
Echo Don't be shy!
Goto 3
:4
Title %Greet% %Nick%!
%Ecko%
Echo Hi %Nick%, My name is Command Prompt
%Ecko%
Pause
%Ecko%
Echo Bye %Nick%!
%Ecko%
Pause

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...