Top 3 Products & Services
1. 2. 3. |
Dated: Aug. 12, 2004
Related Categories
DOSBy Tim Malone
For affordable web design, web hosting, domain registration, and custom programming services, visit Focus Web Design.
Today's tutorial will build on what was taught in last week's tutorial. If you haven't read it yet, see it in Second Batch Tutorial.
Note: If you are running Windows XP, some of the commands shown in this tutorial will not work. You will need to work through this tutorial on a computer running Windows 98 or plain DOS.
Building On The choice Command
We'll get started right away. If you are running in Windows, open up Notepad. If you are running in DOS, open up the MS-DOS Editor (just type edit at the command prompt and press enter). Before you begin, save your file and call it "choice.bat". If you are using Notepad, make sure you select the "All Files" option, otherwise your program will end up being saved as "choice.bat.txt", and it won't open correctly.
Throughout this tutorial, use your file to test out the examples given here. We're not actually going to make anything; we're just testing and learning.
Last week we used choice with the C switch, which lets you decide which options you want the user to choose from. The following switches and options are also available with choice:
N
S
T
"prompt text"
If you don't specify anything for the C switch, choice will assume you are just asking the user a Yes/No question - and the options will be Y/N.
The next switch available for use is N. You'll notice that, on the program you made last week, when you asked for the user's choice, a prompt similar to the following was displayed:
[1,2,3,X]?
It's basically there to remind the user what they can choose from, in addition to what you have already told them about with the echo command. However, if you don't want the user to see that prompt, simply use the N switch. The code you will use will look something like this:
choice /c:123x /n
Then all the user will see is the normal DOS prompt (the flashing underscore _ ) and they can make their decision based on the options you gave them. And, if you didn't give them any options, you'll have one very confused user!
The next switch you can use with choice is the S switch. Personally, this is one I never use, but I'll still tell you about it because someone is likely to use it one day!
The syntax for choice when using the S switch is as follows:
choice /c:123x /n /s
And, all it does is treats the keys that are pressed as case-sensitive. Like when you are typing a password into a webpage - if you use capitals where they aren't supposed to be, your password will be wrong even though it was the right letters. In choice, if you have the case-sensitive switch turned on (the S switch) and one of your options is A and the user presses a, then choice will beep at them as if there is no a option - because there isn't. To get the A, you'd have to hold down shift at the same time.
So, with the S switch, you could potentially have another 26 options the user could choose from! (If you can fit that many on the screen)
The T switch is next in line, and with it you can ask choice to automatically choose an option on behalf of the user if they don't choose an option after a specified amount of time. Here is the syntax:
choice /c:123x /n /s /t:x,30
You'll notice that I'm building each new thing we learn on top of what we know already. Note that you don't have to use every switch, and the switches don't have to be in the same order as I've put them in - with the exception of the "prompt text" option, which you'll learn about soon.
Now, the T switch. See if you can figure out what it is telling choice to do. X is the option that it will choose after 30 seconds - as easy as that. You can change it to whatever you like, just make sure that you don't forget the colon (:) and comma (,) otherwise choice will get a bit confused.
Finally, you can use the "prompt text" option to display a line of text to the user. Of course you could always use the echo command, but this is just another way to do it - and takes up less time to create the program.
All you have to do is enter in the text you want displayed, on the same line as the choice command, but after any switches you have used. So, you'd use something like this:
choice /c:123x /n /s /t:x,30 Please pick an option
A Quick Addition On The echo Command
Last week I told you that the echo command (when typed as echo off with cls on the line after it) will make all the commands you use transparent to the user, and clear the screen. Another way to do it, which doesn't require the cls command, is as follows:
@echo off
Thanks to one of the forum users here at TechiWarehouse for suggesting that.
Another quick snippet of information about the echo command - be careful when you use it alot. Don't use too much of it, otherwise some of the text you have "echoed" will disappear off the top of the screen. If you think you may have used quite alot of echo commands, then make sure you test it out on a conventional DOS system and on a DOS command prompt inside Windows 95 or 98, to make sure that it works in both environments.
Conclusion
And that brings us to the end of the Batch Tutorial series. If you want to learn more, the DOS help program is a great place to start. Just type help at the command prompt and press enter. If you have any problems when creating Batch files, just post a message in our Articles Forum or DOS Forum and one of us will help you out as much as we can.
Happy Batch file creating!
Now that you've gotten free know-how on this topic, try to grow your skills even faster with online video training. Then finally, put these skills to the test and make a name for yourself by offering these skills to others by becoming a freelancer. There are literally 2000+ new projects that are posted every single freakin' day, no lie!
![]() Previous Article | ![]() Next Article |
![]() |