Jump to content

Else statements for MS-DOS?


Recommended Posts

Well, Im quite a novice to the MS-DOS and know most basics on it but I can't find anywhere that explains how to do this,

Basically what I want to do is have the %USER% input a Name, Then the output would be %NAME%

For example,

I've used,

:4
@echo Good, What's your name?
:5
set /p Hello="Type Your Name - "
if "%Hello%"=="Bob" goto :6
@echo Invalid command!
goto :5

If you don't understand please reply and i'll go into more detail.

Link to comment
Share on other sites


SET /P does not exist in MS-DOS.

It exists in NT based systems, starting with Windows 2000.

In MS-DOS you had choice.com or one of the various replacements for it:

http://ss64.com/nt/choice.html

Where would you put the "ELSE" statement? :unsure:

Check this:

http://ss64.com/nt/set.html

http://ss64.com/nt/if.html

See this also:

http://ss64.com/nt/syntax-conditional.html

Read on this site:

http://www.robvanderwoude.com/

http://www.robvanderwoude.com/batchstart.php

http://www.robvanderwoude.com/userinput.php

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Hi, Kube!

Your code is sound, for a NT-family OS DOS Box, however the if statement ought to be without the inverted double commas, like this:

@echo off
(...)
:4
echo Good, What's your name?
:5
set /p Hello="Type Your Name - "
if %Hello%==Bob goto :6
if %Hello%==Kim goto :7
if %Hello%==John goto :8
if %Hello%==Gwen goto :9
echo Invalid command!
goto :5
:6
:7
:8
:9
echo Hi,%Hello%!
(...)

The last two commands in the code snippet do function as the ELSE you're looking for. So, you've alredy found a working solution. In any case, the input will be case sensitive, so that Bob will goto :6, but bOb, BOB, etc, will not.

And if you use @echo off on the first line, you don't need to add @ to the other echo commands.

Link to comment
Share on other sites

With all due respect to dencorso :) something like this seems to me a bit more "modern" (and readable) ;):

@ECHO OFF
SET USERS= Bob Kim John Gwen
:loop
ECHO Good, What's your name?
SET /P Hello=Type Your Name -
FOR %%A in (%users%) DO (
IF /I %%A.==%Hello%. GOTO :user_%Hello%
) ELSE (
ECHO User %%A Not Found!&GOTO :loop
)
)
GOTO :EOF

:user_Bob
ECHO So you are %Hello%, right?
GOTO :EOF

:user_Kim
ECHO So you are %Hello%, right?
GOTO :EOF

:user_John
ECHO So you are %Hello%, right?
GOTO :EOF

:user_Gwen
ECHO So you are %Hello%, right?
GOTO :EOF

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Thanks for all your help, and so fast aswell.

So if im correct

set /p Hello="Type Your Name - "
if %Hello%==Bob goto :6
if %Hello%==Kim goto :7
if %Hello%==John goto :8
if %Hello%==Gwen goto :9
echo Invalid command!

if %Hello%==Bob goto :# - Checks for the input to be Bob then if it is it will go to :#, However if it's not it will go to the second line and check for the name Kim and so on.

Link to comment
Share on other sites

@Kube:

if %Hello%==Bob goto :# - Checks for the input to be Bob then if it is it will go to :#, However if it's not it will go to the second line and check for the name Kim and so on.
Right! And only in case no one of the "if %Hello%==??? goto :#" comands are true then execution will reach the "echo Invallid command! and loop back because of the goto :5.

@jaclaz: your solution *is* very elegant and beautiful. :yes: But, with all due respect to you, it sidesteps the precise question Kube had posed. I tried to cater for that specific question... Then again, I *do* like linear programming with lots of labels and GOTOs. :P

Link to comment
Share on other sites

I'm a little confused about what you mean by an Else statement, (as you can see one isn't required).

:4
Set/p "Hello=Good, What's your name? "
If /i Not "%Hello%"=="Bob" (Echo Invalid Name!&Goto 4)
:5

Link to comment
Share on other sites

Thanks for all your help, I like the other one you posted ut it looks a bit too complicated for me at this time,

Also one more little question, Is there any way to reference their input so that it outputs the word they input,

For example

It'll ask you to type your name,

But because I can't list every name possible I want it to reference the name and then output it,

Would it be

@echo off


:name
set /p Hello="- "
if %Hello%==%Hello% goto :1
echo.
echo.
echo Invalid Command
echo.
echo.
goto :name

:1
echo Hello %Hello%!!!
pause

I think that's correct,

Edited by Kube
Link to comment
Share on other sites

Is there any way to reference their input so that it outputs the word they input,

For example

It'll ask you to type your name,

But because I can't list every name possible I want it to reference the name and then output it

I'm afraid you're not making this easy, it appears that you have an idea of what you wish to achieve, but you're keeping it to yourself and asking for information in small increments so as not to disclose it to us!

Tell us exactly what you are trying to do and perhaps give us the entire script and not just portions of it.

What do you mean by reference their input?

This will ask the question and output the non empty Name, if the Name is empty it will ask the question again. (It uses the Else statement as mentioned in your opening post).

@Echo off&Setlocal enableextensions
:AskName
Cls
(Set/p NameIs=What is your Name? )
If Not "%NameIs%"=="" (Echo Hello %NameIs%!!!) Else (Goto AskName)
Pause

Link to comment
Share on other sites

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,

@echo off
title Hello :P
color b

:1
echo Hello
echo.
echo.
set /p Hello=""
echo.
echo.
if %Hello%==Hello goto :2
if %Hello%==Hi goto :2
echo.
echo.
echo Incorrect command
echo.
echo.
goto :1

:2
echo.
echo.
echo Hi, How are you?
echo.
echo.
:3
set /p Hello=""
if %Hello%==Fine goto :4
if %Hello%==Good goto :4
if %Hello%==Great goto :4
if %Hello%==Cool goto :4
if %Hello%==Bad goto :4
if %Hello%==Cold goto :4
if %Hello%==Hot goto :4
if %Hello%==Weird goto :4
if %Hello%==I dunno you tell me goto :4
echo.
echo.
echo Feeling is not registered.
echo.
echo.
goto :3

:4
echo.
echo.
echo Oh so you're feeling %Hello%? Just wondering what's your name?
echo.
echo.
:5
set /p Hello=""
if %Hello%==%Hello% goto :6
echo.
echo.
echo Invalid Name!!!
echo.
echo.
goto :5
:6
title Hello %Hello%!
echo.
echo.
echo Hi, %Hello%! My name is MS-DOS Command Prompt
echo.
echo.
pause
echo.
echo.
echo Bye %Hello%!
echo.
echo.
pause
exit

Sorry about all the echo's and echo.'s that's just to space things out,

So basically.

It's going to ask you "What's your name?"

Yet, I want it to register each name and then output it, but not output words like hello and jugga and frog, since they're not names, But I doubt that's possible.

Edited by Kube
Link to comment
Share on other sites

I wouldn't even try to use batch files for this.

Batch files are a very rudimentary "script" type that was designed 25+ years ago and improved very little since (added some basic things like FOR loops, and not much more)

Batch files are still great for things like passing command line arguments to apps and the like (really simple tasks) but that's about it. Beyond that, there are better options.

You could try using a simple scripting language (vbscript, jscript, autoit, etc -- powershell is great, but perhaps not very well suited to this particular task), or a "real" programming language -- there are several great options that are absolutely free like these. There's also guides to get you started and all that.

Link to comment
Share on other sites

I wouldn't even try to use batch files for this.

Batch files are a very rudimentary "script" type that was designed 25+ years ago and improved very little since (added some basic things like FOR loops, and not much more)

Batch files are still great for things like passing command line arguments to apps and the like (really simple tasks) but that's about it. Beyond that, there are better options.

You could try using a simple scripting language (vbscript, jscript, autoit, etc -- powershell is great, but perhaps not very well suited to this particular task), or a "real" programming language -- there are several great options that are absolutely free like these. There's also guides to get you started and all that.

Okay thankyou, I also doubt it's possible with Batch anyways :P Im just seeing what's possible with batch and stuff, :) Thanks for all your help. :)

Link to comment
Share on other sites

You cannot possibly list every known feeling nevermind all real names!

Is there a particular reason why you cannot output a list/index of responses to choose from instead of trying to achieve the impossible?

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...