Rob van der Woude's Scripting Pages

(Ab)using the PROMPT command

As said before, the most commonly used prompt string is probably $P$G, which results in a prompt like this:


As you can see, this is one way of showing the ">" character on screen.
If you would try to use ECHO to show the ">" character on screen, this would result in erroneous redirection of part of the echoed text to a file:


will result in a file named DIR which contains the text "C:\".


will result in an error message either stating that there are too many parameters (DOS), or that it cannot create a file with a wildcard in its long filename (the command would try to create a file named "DIR *.BAT").

The ECHO command obviously isn't the right way to show < or > characters on screen — at least not in a COMMAND.COM session; in NT's and OS/2's CMD.EXE these characters can be escaped using carets ( ˆ ):


will display the text C:\>DIR *.BAT on screen.

In DOS (COMMAND.COM) we'll have to use the PROMPT command instead.
As we've seen before, $L shows a < character and $G a >.
So the following batch file would show some HTML tags on screen:

REM Use temporary prompt to display greater than
REM and less than characters on screen

REM Restore default prompt

The empty line between the ECHO ON and the second @ECHO OFF line causes the prompt to be actually displayed:


This trick may not work in OS/2 or NT, however. So instead of an empty line you could use %COMSPEC% /C without any further parameters, or %COMSPEC% /K EXIT to display the prompt. Using %COMSPEC% instead of CMD.EXE makes the batch file more versatile, since you leave it to the operating system to fill in the actual command processor. This way you can start the batch file in an NT or OS/2 command prompt as well as in a DOS emulation window.

Combined with redirection to temporary batch files this is an extremely powerful technique to perform complex and near-impossible tasks with "plain simple DOS".
This combination of methods is the secret behind our extreme batch file example.

A lot of trial and error led me this to this more or less operating system independent method to redirect "unprintable" characters to files:

ECHO EXIT XYZ | %COMSPEC% /K | FIND /V "" | FIND /V "EXIT XYZ" > TempFile.html

This will create a file TempFile.html with the following content:


Instead of XYZ following EXIT, any "unique" string can be used. Just make sure it is matched by the second FIND /V command (and, of course, that the first word is still EXIT).

In the following example, where only one single line is redirected, the relevant line is filtered by searching for a "unique" character or string within that line.

PROMPT $_NewFiles$Q$Lnone$G$_
ECHO EXIT | %COMSPEC% /K | FIND "=" > MyIni.ini

This will create a file "MyIni.ini" with the following content:


These last two examples will work in Windows 9x, NT 4 and 2000, and probably (!) in XP as well.

Note:   Check your batch files in every possible DOS environment they are likely to encounter.
The fact that $H, when redirected, cannot be used as a backspace (only in MS-DOS 7.*) cost me several days of debugging when creating my CDROM batch file.

page last modified: 2011-11-18; loaded in 0.0063 seconds