home *** CD-ROM | disk | FTP | other *** search
- ASK
-
-
- ASK - allows batch file to prompt user interactively
-
-
- Syntax
-
- ASK [Options] [Prompt] [Expected response]
-
-
- Parameters
-
- [Prompt] is the question to be displayed; it is a "quoted string"
- (double quotes only).
-
- [Expected response] is a string of all the different characters
- that the user may press in response to your question. If the user
- press the first character in the [Expected response], the
- errorlevel will be set to 1; if the user press the n-th character
- in the [Expected response], the errorlevel will be set to n. If
- the user pressed an invalid response, ASK will prompt him/her
- again until she/he gets it right.
-
- By default, letters in the [Expected response] will match either
- UPPER or lower case (i.e. case insensitive).
-
- If the [Expected response] string is not present, the errorlevel
- will be set to the character code of whatever key the user
- pressed. Normally lower case letters will be mapped to the upper
- case counterpart, i.e. 'a' and 'A' will both return 65.
-
-
- Options
-
- There are two options:
-
- /Q - be quiet, don't yell at the user for unexpected
- response; but return 0 as the errorlevel. This option is
- meaningless if there is no [Expected response].
-
- /C - make [Expected response] case sensitive. This means
- letters typed by the user will match letters only in the
- same case in [Expected response]. If there is no
- [Expected response], then UPPER case letters will return
- errorlevel 65-90, and lower case letters will return 97-
- 122.
-
- More than one option letter can share a common switch, like this:
-
- ask /QC "Blah Blah? " or ask/qc "Blah Blah? "
-
- You can also do this:
-
-
- - 1 -
-
-
-
-
- ASK
-
-
-
- ask /c/q "Blah Blah ?"
-
- Option letters can be either upper or lower case.
-
-
- Special Characters
-
- Characters that you normally cannot put on a command line (e.g.
- carriage return, escape, backspace, ...) can be included in
- either the [Prompt] or the [Expected response] by using either
- one of the following conventions:
-
- ^nnn - translates to ASCII nnn where nnn is a 3-digit decimal
- number. If this is not followed by another digit (that
- you happen to want to appear in the prompt) then you
- don't need to put all three digits down. If it is
- followed by a digit, then you have to put down all
- three digits (add leading zeros if necessary).
-
- ^a - where a is anything except 0-9, ", and ^. This will
- translates into the corresponding control character.
-
- ^^ - the ^ (caret) character itself
-
- ^" - the double quote character. Without the caret it will
- be interpreted as the beginning or the end of a quoted
- string.
-
- Here are some examples:
-
- ask "How are ^"U^"?"
-
- - will produce How are "U"?
-
-
- ask "Hi?^7" or
- ask "Hi?^g" or
- ask "Hi?^G" or
- ask "Hi?^007" (this has nothing to do with spies)
-
- - will produce Hi?*BEEP*
-
-
- ask "press Enter key when ready " ^m
-
- - will wait for the user to press the enter key
-
-
- ask "1=egg 2=apple 3=orange^m^jSelect one? " 123
-
-
- - 2 -
-
-
-
-
- ASK
-
-
-
- - will print "1=egg 2=apple 3=orange" on one line and
- "Select one? " on another one because of ^m^j
- (carriage return plus line feed).
-
-
- Detailed Description
-
- ASK is a program to let batch files accept a one-character
- response from a user. This allows you to make simple yes-or-no
- questions or menu-driven type questions.
-
- When testing errorlevel, be sure to test for the highest number
- first because
-
- if errorlevel 5
-
- means
-
- if errorlevel equal to or greater than 5.
-
- For example, you can put the following in AUTOEXEC.BAT:
-
- echo off
- ASK "Load ForKicks (Y/N)? " yn
- if errorlevel 2 goto no
- forkicks
- :no
- echo on
-
- When you boot the computer, you will see the following (minus the
- frame):
-
- +-----------------------------------------+
- | Load ForKicks (Y/N)? _ |
- +-----------------------------------------+
-
- If you press 'y' or 'Y', the batch file will then execute the
- program forkicks; if you press 'n' or 'N', you will return to DOS
- without executing forkicks.
-
- Another use of ASK is to give the user a choice between different
- versions of the same program. Let's say a new version of EDLIN
- just arrived, you can name the old one EDLIN1 and name the new
- one EDLIN2, and put the following in EDLIN.BAT:
-
-
-
-
-
-
-
- - 3 -
-
-
-
-
- ASK
-
-
- echo off
- echo 1) EDLIN old version
- echo 2) EDLIN new version
- echo rem ** there's an Alt-255 character after the echo
- ask "Choose one (press 1 or 2)? " 12
- if errorlevel 2 goto two
- echo on
- edlin1 %1
- goto end
- :two
- echo on
- edlin2 %1
- :end
-
- Now when a user type EDLIN, she/he will get this menu:
-
- +-----------------------------------------+
- | 1) EDLIN old version |
- | 2) EDLIN new version |
- | |
- | Choose one (press 1 or 2)? _ |
- +-----------------------------------------+
-
- This way you wouldn't have to tell everybody that a new version
- of EDLIN had been installed, and after a while people will learn
- to type EDLIN1 or EDLIN2 to get to their favorite editor.
-
- The return key can be used as a valid response by including ^m in
- [Expected response]. This can be used to provide default
- response. For example
-
- echo off
- ask "Should I do it or not (Y/N) [default Y]? " ^myn
- rem ** errorlevel = 3 means n key
- rem ** errorlevel = 2 means y key
- rem ** errorlevel = 1 means return key
- if errorlevel 3 goto no
- DoIt
- :no
- echo on
-
- Now if the user press either 'y' or the enter key, the computer
- will DoIt.
-
- Here is a trival use for ASK:
-
- ask "press any key when ready"
-
- This will emulate the 'pause' command in DOS.
-
-
-
- - 4 -
-
-
-
-
- ASK
-
-
- If you supply the [Expected response] characters and the user
- press something unexpected, ASK will automatically address the
- user
-
- *BEEP* Unexpected response - try again
-
- and prompt the user again until he/she press a key that is in the
- [Expected response].
-
- If you don't like this, you can set the /Q (quiet) option and
- trap the unexpected response yourself (it will be errorlevel 0).
- For example:
-
- echo off
- :again
- echo 1 - run hack
- echo 2 - run baby
- ask/q "Select one (1 or 2)? " 12
- if errorlevel 2 goto baby
- if errorlevel 1 goto hack
- rem *** this must be errorlevel 0 ***
- echo No, you must press either 1 or 2. Please try again.
- goto again
- :baby
- baby
- goto end
- :hack
- hack
- :end
- echo on
-
- Normally ASK will consider upper case and lower case letters
- equivalent. In some cases you might want to know whether the
- user press a lower case letter or an upper case letter. You can
- use the /C (case sensitive) option, like this:
-
- echo off
- echo a - run apl
- echo A - run APE
- ask/c "Select one (a or A)? " aA
- if errorlevel 2 goto ape
- apl
- goto end
- :ape
- APE
- :end
-
-
- Author
-
- Peter Wu
-
- UUCP: {akgua|allegra|harvard|ihnp4|seismo|topaz|ucbvax}
- !uwvax!uwmacc!pwu
- ARPA: pwu@unix.macc.uwisc.edu or peter@gumby.uwisc.edu
-