[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 649
  • Last Modified:

Dos Batch programming-Command line menus


         I have a user who wants to work down on the dos command
line rather than up at the graphic level for some tasks.   For this she
would like a little menu which would allow her to change to different
directories and start different programs based on inputting a
number from 1 to 9.
       Now this is ancient Dos batch programming and I just can't
seem to make the backwards leap to understand how to  write
the little file needed.   Can someone give me a hand on this?

0
bwsaul
Asked:
bwsaul
1 Solution
 
Jason_SCommented:
You will probably need to use the Choice command to accomplish this.

C:\>choice /?
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]

/C[:]choices Specifies allowable keys. Default is YN
/N           Do not display choices and ? at end of prompt string.
/S           Treat choice keys as case sensitive.
/T[:]c,nn    Default choice to c after nn seconds
text         Prompt string to display

ERRORLEVEL is set to offset of key user presses in choices.

Otherwise, you may be able to find another menuing program to do this.
0
 
joe_hCommented:
Okay, here's an example for your little batch...

_________CUT HERE___________
@echo off
:MM
cls
echo Select the app to run
echo ----------------------
echo 1 Application One
echo 2 Application Two
echo 3 Yet Another application
echo --------------------
echo E Exit this menu

choice /C:123E Your choice

if ERRORLEVEL 4 goto :A0
if ERRORLEVEL 3 goto :A3
if ERRORLEVEL 2 goto :A2
if ERRORLEVEL 1 goto :A1

:A1
echo Starting app 1 .....
c:\wherever\the\app\is\app1.exe
goto MM

:A2
echo Starting app 2....
call d:\somewere\else\is\app2.bat
goto MM

:A3
echo Starting app 3....
something.com
goto MM

:A0
echo Exitting, bye ........

____________CUT HERE_______________

It is really straightforward, for adding more applications just remember that the if ERRORLEVEL n statements need to be in DESCENDING order (the command jumps if the errorlevel is equal to or greater than the number specified).
The choice command is not limited to numbers only. I think it is better to use 'A' for Accounting, 'E' or 'Q' for Exit/Quit etc.

Instead of/ before starting an application, you can do e.g.
e:
cd \accounting\1999

Feel free to post a comment if you need any more help with this.
0
 
joe_hCommented:
OOOOOOPPSSS! Sorry, the lines below choice... should read:

if ERRORLEVEL 4 goto A0
if ERRORLEVEL 3 goto A3
if ERRORLEVEL 2 goto A2
if ERRORLEVEL 1 goto A1

(that is, without the colons....)
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bwsaulAuthor Commented:

       It worked like a charm.


                        Thanks!
0
 
joe_hCommented:
You're welcome... thanks for points :))
0
 
FreezingPeteCommented:
Hi,

I've been working on a similar batch and as I'm using Windows XP - have had to resort to the SET command in place of the CHOICE command which isn't available.

@echo off
:MM
cls
echo Select the app to run
echo ----------------------
echo 1 Application One
echo 2 Application Two
echo 3 Yet Another application
echo --------------------
echo E Exit this menu

set /p userinp=

if %userinp%==4 goto A0
if %userinp%==3 goto A3
if %userinp%==2 goto A2
if %userinp%==1 goto A1

And then the rest like show on joe_h's example above.

Also, I found applying the /I variable on the IF command quite useful when prompting a user for a Yes or No choice - it ensures the string is Case- Insensitive; meaning that if they are prompted to press Y or N, it doesn't matter if they type y or n.
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now