Saturday 12 March 2011

How to create a menu driven DOS program

For those who are wondering how to make some sort of DOS(command prompt) program that continues to allow the user to make commands until they want to quit. We can create a menu driven DOS program. At the end of this tutorial i have a an example of what one can look like.




First lets go to the environment you will make such a program in. Such an environment is simply any text editor. So open up a text editor. Notepad will suffice if you have it. This is where you will type your commands.

Commands:
 The "CLS" command. This command clears the screen of writing. Very important if you want to clear up and make your program more readable or look neater.

The "ECHO" command. This command will allow you to write to the screen. As an example(Ex).

Ex.
ECHO Hello World. Will print Hello World to the screen.
A helpful tip is ECHO. <---period after echo just prints a newline.

The "GOTO" command. This command is most important in this example of the menu driven program. This command is also good if you don't want your program to close as soon as it runs like a DOS program would.

The GOTO can be used to as it says GOTO a certain tag in the program. A tag being lets say :start, and :end.
 To create a tag in your program just put a ":" before the name of the tag.

Ex.
:start
ECHO Hello World
GOTO start
 :end 

This program will go into an infinite loop. For the program will read the line :start, the print Hello World, then GOTO the tag start,from here it will print Hello World, then GOTO the start tag again. You get the idea.

Problem for you. How you would you have this program skip the lines ECHO Hello World, and GOTO start?

If you said put GOTO end, before the echo statement. You are correct!

The "IF" command. This command will allow you to execute statements/commands depending on if a certain condition you set in your IF command is true. We will touch on this after i show you the set command.

The "SET" command. This command will allow you to SET a variable to equal something. For our case we want to use it to create a variable and allow the user to enter a value at choice. For this we use the " /p" statement with SET. As an example

Ex.
SET /p M=

The above example will wait until the user enters a value for the newly created variable M.
 Variables are created and used as data to hold and store values.
You can also have info for your user after the SET command.

Ex.
 SET /p M=Enter a value for some variable M then press Enter:

Now back to the IF command. An example of this command can be seen in the below example.

Ex.
 SET /P=M

IF %M% ==  69 GOTO :start

This example shows the use of how to access a variable and how to write a condition. The condition in the example is shown by %M% == 69. Lets explain whats going on here. The %M% code is how we access the variable M and use it in our condition. The == code is to see if there is an equivalence to the variable M and the number 69. So as the example is stating. IF the variable M is equal to 69 THEN GOTO :start.

These commands are all you will need to use to make a menu driven DOS program.
Before i show my final DOS program for this blog. I will now explain the use of two more commands.

The "START" command. This command is used to well START a file. Now in order to do this we need to be in the same working directory as the file(this is a little more complicated for this tutorial).

Ex. START Minecraft.exe

The "CD" command. This command stands for change directory. Lets say your working directory is
C:\user\somename\, and you want to move to just the D drive. Then you would type:

Ex.
CD D:

Now after learning about all these commands and writing crazy code into a text file . How to we get this program to run. Because honestly DisposedCheese i think your crazy because when i click on the program it just opens the text editor again  to edit.

Well now what we do is we want to change the extension of our text file to a .bat. This convert the  text file into a batch file. A batch file will run using command prompt and execute your lines of code accordingly from top to bottom.

As a result the final menu driven DOS program. Feel free to copy and change things accordingly to make your own. You might want to take the START and CD command out. Also you may want to read the code and see if you can make sense of what is going on. As well as what happens when you enter certain values at the prompts.

Ex Menu Driven DOS Program


CLS
:MENU
ECHO.
ECHO DisposedCheese's Menu
ECHO.
ECHO.
ECHO Entering 0 continues.
ECHO Entering 1 quits.
ECHO Entering 2 starts minecraft.
SET /P M=Type 0, 1 or 2, and enter:
CLS
IF %M%==0 GOTO ZERO
IF %M%==1 GOTO ONE
IF %m%==2 GOTO TWO
IF %M%==9 GOTO END
:ZERO
CLS
ECHO i am glad you wanted to continue.
GOTO MENU
:ONE
CLS
ECHO Why you want to end the batch, too bad.
ECHO if you want to quit type 9 at the prompt.
GOTO MENU
:TWO
CLS
CD C:\some directory\desktop
START Minecraft.exe
GOTO MENU
:END

I hope you enjoy this.

15 comments:

  1. very useful for my systems class

    ReplyDelete
  2. Sweet a place to learn abit of coding nothing like new knowledge to gather when bored

    ReplyDelete
  3. Good thing I found this blog, I need this.

    ReplyDelete
  4. Wow; this information really helped me and my roommate with some stuff we have been working on.

    ReplyDelete
  5. I'm in intro to computer class i'll show the teacher what I can do with this :D

    ReplyDelete
  6. My friend would create batch files at school all the time. They would basically create endless folders if someone ran them. It usually resulted in their account getting suspended too it was awesome.

    ReplyDelete
  7. great tut! looking forward to more :)

    ReplyDelete
  8. really helpful tutorial
    it's hard to find someone who explains things good like this.

    ReplyDelete
  9. hah this is some fun stuff to mess around with:P
    using the goto command you can create a batch file that loops random text infiniately. Do that, and fullscreen it at bestbuy and it looks like the computer is broken:P

    ReplyDelete
  10. Thats for the tutorial. Followed! alphabetalife.blogspot.com

    ReplyDelete
  11. Very interesting post I am going to have to check this out sometime

    ReplyDelete
  12. cooooool coding ill have to check this out

    ReplyDelete