3

I asked this question on Stack Overflow, people said it'd more appropriate to ask it here.

I've got to create a batch file that will run with DosBox. The program should change the color of the text by asking user to enter name of the color. Here's a code of what I've done so far for you to better understand:

@echo off

:start
cls
echo COLOR TEXT
echo.
choice /C12/S/N Choose color of the text (1 - red 2 - yellow)  
if errorlevel 2 goto yellow
if errorlevel 1 goto red


:red
echo ←[42;31;40m
cls
echo COLOR TEXT
echo.
choice /C12/S/N Choose color of the text (1 - red 2 - yellow)  
if errorlevel 2 goto yellow
if errorlevel 1 goto red


:yellow
echo ←[42;33;40m
cls
echo COLOR TEXT
echo.
choice /C12/S/N Choose color of the text (1 - red 2 - yellow)  
if errorlevel 2 goto yellow
if errorlevel 1 goto red

In this program color of text is controlled by pressing keys (1 - for red, 2 - for yellow). But is there any way to create a program (without using external utilities) that would control color by user entering name of the color (e.g. for setting red color user would have to input "red" and press enter, for yellow - "yellow" and so on).

I used command "choice", but it only allows to press keys, not to input text. I know there's a command "set /p" for batch files, but it doesn't seem to work for MS-DOS.

Is there anyone who could help me, please?

Thanks in advance!

9
  • 1
    In addition to Solomon’s question, would it be acceptable to use a more advanced shell than the default (especially in DOSBox), like 4DOS? May 4, 2022 at 19:24
  • 1
    Thanks for the replies, everyone! 1) I think helper program would be acceptable! Could you please let me know how to create such a program? 2) No, unfortunately, I can only use DosBox. 3) ANSI sequence worked for me. I know it needs to have an escape before "[", the thing is my post has been automatically modified and the symbol before "[" has been removed.
    – Rimanio
    May 4, 2022 at 19:53
  • 2
    set /p and set /a are 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
    – phuclv
    May 5, 2022 at 16:45
  • 2
    Off competition: A back then common solution would have been to make a dedicated batch file for every colour, so the user just types YELLOW or RED to get the text colour he wants. Simple, straight and quite extendable. I have seen small adventures written that way :))
    – Raffzahn
    May 5, 2022 at 18:21
  • 1
    I would strongly recommend performing your task (all of it or just this part) with a little program made in Quickbasic or something. As you can see, string input is out of the reach of batch files in DOS and Dosbox. :)
    – knol
    May 6, 2022 at 1:22

2 Answers 2

4

Here's one way to do it in DOSBOX 0.74-2. I would definitely suggest not using this method, and using CHOICE instead, but it's here if you need it.

First you need to make a file called USR.TMP that will contain the following text.

SET USERCOLOUR=

There must be no newline at the end of this file for this to work. You have to verify this with a hex editor.

Here is the script that accepts and compares text input:

@ECHO OFF
ECHO Enter your colour:
COPY USR.TMP+CON USR.BAT>NUL
CALL USR.BAT

ECHO You entered %USERCOLOUR%.
IF %USERCOLOUR% == red GOTO code_red
IF %USERCOLOUR% == yellow GOTO code_yellow
ECHO Unknown colour.
GOTO code_end

:code_red
ECHO ←[42;31;40m
ECHO Bricks are red.
GOTO code_end

:code_yellow
ECHO ←[42;33;40m
ECHO Sunflowers are yellow.
GOTO code_end

:code_end
ECHO ←[0m
ECHO End of batch file.

The line COPY USR.TMP+CON USR.BAT>NUL combines the SET USERCOLOUR= line with the text your user is entering to create the full batch file into USR.BAT, and suppresses the normal 'n files copied' output.

enter image description here

Limitations: your users can't see what text they're entering.

3
  • 2
    Didn’t mention this under the other answer, but: it’s good practice to put quotes " around both the environment variable reference and the literal string it’s compared against. Otherwise COMMAND.COM may fail in the case where the variable is absent. May 6, 2022 at 5:25
  • Thank you so much for your help, it is what I was looking for! I really appreciate all the time and effort you spent on this!
    – Rimanio
    May 6, 2022 at 10:16
  • @user3840170 Very true. Though in this case this answer is specifically about the behaviour of DOSBOX's shell, not COMMAND.COM. But either way we're discussing what golf club is best for doing DIY with. :)
    – knol
    May 6, 2022 at 19:43
10

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 OFF
ECHO Enter your colour:
ECHO SET USERCOLOUR=%%5>ENTER.BAT
FC CON NUL /LB1 /N | DATE | FIND " 1: " > TEMP.BAT
CALL TEMP.BAT
DEL TEMP.BAT
DEL ENTER.BAT

ECHO You entered %USERCOLOUR%.
IF %USERCOLOUR% == red GOTO code_red
IF %USERCOLOUR% == yellow GOTO code_yellow
ECHO Unknown colour.
GOTO code_end

:code_red
ECHO ←[42;31;40m
ECHO Bricks are red.
GOTO code_end

:code_yellow
ECHO ←[42;33;40m
ECHO Sunflowers are yellow.
GOTO code_end

:code_end
ECHO ←[0m
ECHO End of batch file.

The character 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 TEMP.BAT and 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.

https://www.robvanderwoude.com/userinput.php

They describe a hack using COPY with 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 . . .
ECHO ←[13;0;64;13p
COPY CON USRINPUT.TMP
ECHO ←[13;13p
CLS
ECHO You typed:
TYPE USRINPUT.TMP

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 DATE.

9
  • 3
    Wow, I’m not sure whether to be impressed by this FC trick or disgusted by it. Then though, the same could be probably said about everything else in MS-DOS. May 5, 2022 at 15:01
  • 2
    You might want to make a note that ‘←’ is supposed to represent byte 0x1B, not U+2190. May 5, 2022 at 15:03
  • @knol Thanks a lot for such a detailed answer! I tried to run a batch file with code you sent using DosBox and it says that "FC" command is illegal. Was this code you sent supposed to run with DosBox?
    – Rimanio
    May 5, 2022 at 16:08
  • 2
    @Rimanio yes, FC is included in MS-DOS 2+
    – phuclv
    May 5, 2022 at 16:43
  • 1
    @Rimanio except, making it work/asking for DOSbox isn't really on topic here, as DOSbox is a recent piece of software. DOSbox problems are only as far on topic as they are the same as with real DOS. Knol's answer is fine as it's about the real stuff.
    – Raffzahn
    May 5, 2022 at 18:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .