Help - Search - Members - Calendar
Full Version: Else statements for MS-DOS?
MSFN Forums > Coding, Scripting and Servers > Programming (C++, Delphi, VB, etc.)

   


Google Internet Forums Unattended CD/DVD Guide
Kube
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,

CODE
: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.
jaclaz
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.gif

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
CoffeeFiend
I was thinking he meant "command prompt" instead of MS-DOS (whereas batch files would have been more appropriate anyways)

I mean, what's the odds he's using a pre-Win95 OS that's 15 years old or more?
dencorso
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:

CODE
@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.
jaclaz
With all due respect to dencorso smile.gif something like this seems to me a bit more "modern" (and readable) newwink.gif:
CODE
@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
Kube
Thanks for all your help, and so fast aswell.

So if im correct

CODE
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.
dencorso
@Kube:
QUOTE (Kube @ Jul 25 2009, 06:58 AM) *
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.gif 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. tongue.gif
Yzöwl
I'm a little confused about what you mean by an Else statement, (as you can see one isn't required).
CODE
:4
Set/p "Hello=Good, What's your name? "
If /i Not "%Hello%"=="Bob" (Echo Invalid Name!&Goto 4)
:5
Kube
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

CODE
@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,
Yzöwl
QUOTE (Kube @ Jul 25 2009, 02:07 PM) *
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).
CODE
@Echo off&Setlocal enableextensions
:AskName
Cls
(Set/p NameIs=What is your Name? )
If Not "%NameIs%"=="" (Echo Hello %NameIs%!!!) Else (Goto AskName)
Pause
jaclaz
QUOTE (Kube @ Jul 25 2009, 03:07 PM) *
QUOTE
if %Hello%==%Hello% goto :1



I think that's correct,


Well, if you can show me an example of a case where %Hello% will be different from %Hello%....newwink.gif

http://en.wikipedia.org/wiki/Tautology_(logic)

jaclaz
Kube
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,

CODE
@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.
CoffeeFiend
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.
Kube
QUOTE (CoffeeFiend @ Jul 25 2009, 01:28 PM) *
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 tongue.gif Im just seeing what's possible with batch and stuff, smile.gif Thanks for all your help. smile.gif
Yzöwl
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?
MHz
QUOTE (Kube @ Jul 26 2009, 04:15 AM) *
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.
CODE

@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.
Scr1ptW1zard
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).

CODE
@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:

CODE
@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
Yzöwl
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.
CODE
@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
jaclaz
Isn't this slowly drifting away from elementary usage of available commands in batch files to A.I. algorithms in BATCH? whistling.gif

tongue.gif

jaclaz




Google Internet Forums Unattended CD/DVD Guide

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.