In MS-DOS, the following batch file will prompt the user for a single word of input and store it in the
%USERCOLOUR% environment variable where you can use
IF to test for your desired values.
ECHO Enter your colour:
ECHO SET USERCOLOUR=%%5>ENTER.BAT
FC CON NUL /LB1 /N | DATE | FIND " 1: " > TEMP.BAT
ECHO You entered %USERCOLOUR%.
IF %USERCOLOUR% == red GOTO code_red
IF %USERCOLOUR% == yellow GOTO code_yellow
ECHO Unknown colour.
ECHO Bricks are red.
ECHO Sunflowers are yellow.
ECHO End of batch file.
← is byte 0x1B (appears as a leftward pointing arrow in MS-DOS EDIT) and can be entered by pressing Ctrl+P and then holding Alt and typing 2 7 on the numeric keypad.
Be aware that this will clobber both
ENTER.BAT in the current working directory. The former can be changed to
%TEMP%.\TEMP.BAT, which should work regardless of whether
%TEMP% ends in a backslash.
ENTER.BAT cannot be changed because it relies on the predictable output of the existing MS-DOS commands being interpreted as a command-line command.
I got this from YEW KWANG HOOI's page at http://www.geocities.ws/jawsyew/BatchProcessing.html
Here's their explanation:
FC and DATE
We use the CON device to get user input. But instead of trying to define the end of con by adding a ctrl-z, we switch viewpoints and define when we will quit reading con. The FC command can do this with it's /LB option. The trick is to use FC not to compare files (which is what FC is made for), but to compare the CON device with the NUL device.
Now, the NUL device has nothing in it, so when FC compares NUL to CON, the difference will always be exactly what the user keys into CON. Duh. But FC's /lb option allows us to specify how many different lines will be accepted. With /lb1 specified, FC will quit reading con after the first different line (which will be the first line). All the user has to do is hit "Enter" to define the end of the line.
We also use FC's /n (line numbering) option for two reasons: First, FC puts out quite a few lines. Having it number the lines makes it easy to FIND the line we want. Second, we'll be putting the output of FC into the input of DATE (wonder why?). By numbering the line, we can allow for the otherwise embarrassing problem of having the user enter as a first word something that might be interpreted as a date. The first word will be "1:", which is not a date.
Now it would be trivial to use FIND to extract the desired line by searching for "1:". But I want you to notice something else. FC always adds a blank line to it's output. This is going to come in real handy, because next I'll pipe the output of FC into DATE.
If you've ever tried to set the date, you've noticed how persistent DATE is. It will keep asking you to enter a new date until you either enter a date or until you just press Enter. Since we never (in this example) give DATE a valid date, it will keep rejecting our lines until it hits the blank line at the end of FC's output. If you count, you'll see my single line has resulted in twenty lines of output from DATE (6 of which are blank). By piping DATE's output through FIND looking for "1:", we'll end up with just one line.
Now, if we were to take that line and call it a batch file (Which I'll call TEMP.BAT), when we ran it it would try to execute the ENTER command (Since "Enter" is the first word on the line). Luckily, there is no "enter" command, so we can write our own batch file called ENTER.BAT. When TEMP.BAT runs, it will run our ENTER.BAT and pass new date (mm-dd-yy): 1: this is a test to ENTER.BAT as arguments. Notice how "this" (The first word I typed) is the fifth argument ("new" is first, "date" is second, etc.).
Previous answer which I couldn't get to work:
I've found a website which describes ways of getting text input in Microsoft batch files in all versions of Windows, all the way back to various versions of MS-DOS.
They describe a hack using
CON to take console output and write it to a file. This method has a restriction where the input has to be terminated with Ctrl+Z or F6 before the Return key, but there is a second hack which eliminates this requirement.
The following trick uses ANSI to perform some key translation: the Enter key is translated to the F6 key followed by the Enter key. Thus only one line of input can be entered, and pressing the Enter key sends the input to the temporary file USERINP.TMP.
ECHO Enter some input, and press Enter when ready . . .
COPY CON USRINPUT.TMP
ECHO You typed:
The file created using
COPY can be read and finessed into a batch file that uses
SET to put the inputted text into an environment variable so decisions can be made on it. I didn't manage to get this to work as I believe the output of
DATE might differ in MS-DOS 6.0 (which I assume they're using) and 6.22 (which I have)?
You can go to the linked site or look at the edit history of this answer to see the code that uses
COPY CON and
set /aare internal commands of cmd.exe which is a completely different shell from command.com, so obviously it can't work in DOS. They're very different things but unfortunately many people call cmd "DOS" which makes searching for real commands for the crippled DOS even more difficult
REDto get the text colour he wants. Simple, straight and quite extendable. I have seen small adventures written that way :))