MHz Posted July 26, 2009 Share Posted July 26, 2009 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 offtitle Hellocolor bREM Greeting code sectionecho Script: Helloset /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 sectionecho 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 sectionecho Script: What is your name?set /p Hello="User: "if "%Hello%"=="" ( echo Script: Invalid Name!!!) else ( call :sub_5 %Hello%)set Hello=pauseexitREM Subroutine code section:sub_2 echo Script: Thankyou for typing %1goto :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 :eofNote: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 More sharing options...
Scr1ptW1zard Posted July 26, 2009 Share Posted July 26, 2009 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:topset /p %name%=Enter Name:echo Hello, %name%call :%name% 2>nulif 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 :eofTo carry this one step further, to retrieve the user's feeling:@echo off:topset /p name=Enter Name: echo Hello, %name%call :%name% 2>nulif errorlevel 1 ( echo Bad name goto top):getfeelingset /p Feeling=How are you feeling %name% (Fine, Good, Great)? echo %name% is feeling %feeling%...call :_%feeling% 2>nulif 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 :eofI 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 More sharing options...
Yzöwl Posted July 26, 2009 Share Posted July 26, 2009 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&SetlocalTitle Hello :PColor bSet "Ecko=Echo/&Echo/":1Set/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:4Title %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 More sharing options...
jaclaz Posted July 26, 2009 Share Posted July 26, 2009 (edited) Isn't this slowly drifting away from elementary usage of available commands in batch files to A.I. algorithms in BATCH? jaclaz Edited July 26, 2009 by jaclaz Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now