eg echo <acii for ESC> > filename.txt
38) Is it possible to echo the redirection symbol in a batch?
(goes for the esc as well)
230849 Oct 27 2002 ftp://garbo.uwasa.fi/pc/link/tsbat.zip
tsbat.zip Useful batch files and tricks, linked from /pc/ts, T.Salmi
All the best, Timo
--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html
Yes, here are two ways (and a third curiosity):
(a) The PROMPT method
You can use a specialised PROMPT command. The template below shows how to
use a Batch Subroutine call to ECHO a PROMPT command, in this case $E (the
Escape character) and $G (the Greater-than character). The demo assumes
you'll pass the output filename as the first parameter. The output file
contains an initial CarriageReturn LineFeed combo, which can be filtered
out with FIND. The advantage of the Batch Subroutine approach is that you
don't have to create an intermediate workfile to ECHO the prompt.
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF (GOTO:)==(%1) %1%2 {Subroutine-Handler}
IF ()==(%1) GOTO:EOF
%COMSPEC%/e:2048/c %0 GOTO: _SPECIAL>%1
GOTO EOF {=Subroutine-section-below=}
:_SPECIAL
PROMPT $E$G
ECHO ON
@ECHO OFF
:EOF {End-of-file}
====End cut-and-paste (omit this line)
Win9x GUI study/demo only. Cut-and-paste as Batch script (file with .BAT
extension).
The blank line in the Subroutine (between ECHO ON and @ECHO OFF) is what
ECHOes the special PROMPT command. Because the PROMPT is changed in a
child shell (launched by the %COMSPEC% command), there's no need to
restore the current prompt (it's restored automatically as the child shell
closes. For details of character codes in the PROMPT command use:
prompt /?
This screen capture shows operation, and uses DEBUG to show the characters
that the output file contains:
============Screen capture Windows 95
C:\WORK>demo1.bat file1.txt
C:\WORK>
C:\WORK>debug file1.txt
-d100 l6
0F19:0100 0D 0A 1B 3E 0D 0A
-q
C:\WORK>
============End screen capture
The ESC character is 1B hex and the > character is 3E hex. 0D 0A is the
CarriageReturn Linefeed combo.
(b) Using the CSCRIPT Batch interface of WSH
If you have any version of WSH installed (almost all Windows machines, see
note 1) you can use the special wscript.echo command to ECHO characters by
their decimal ASCII codes, for example as chr(27) for ESCape and chr(62)
for the redirection operator >
For combinations of characters, use & in between. This demo shows the
simple syntax. Again, it assumes the output file will be supplied as the
first parameter. It uses a temporary workfile _TEMP.VBS to pass the
wscript.echo command to CSCRIPT:
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF ()==(%1) GOTO:EOF
ECHO.wscript.echo chr(27)&chr(62)>_TEMP.VBS
cscript//nologo _TEMP.VBS>%1
DEL _TEMP.VBS
:EOF
====End cut-and-paste (omit this line)
Win9x GUI study/demo only. Cut-and-paste as Batch script (file with .BAT
extension). Lines that don't begin with 2 spaces have wrapped by mistake.
In this case, the output file contains only the characters Escape and the
Redirection operator followed by a CarriageReturn Linefeed. No cleaning up
of the output file is needed.
============Screen capture Windows 95
C:\WORK>demo2.bat file2.txt
C:\WORK>
C:\WORK>debug file2.txt
-d100 l4
0F19:0100 1B 3E 0D 0A .>..
-q
C:\WORK>============End screen capture
To include other text along with special characters, use the & string
concatenation operator and "quote" the other text:
====Begin cut-and-paste (omit this line)
@ECHO OFF
IF ()==(%1) GOTO:EOF
ECHO.wscript.echo chr(27)&" other text "&chr(62)>_TEMP.VBS
cscript//nologo _TEMP.VBS>%1
DEL _TEMP.VBS
:EOF
====End cut-and-paste (omit this line)
Win9x GUI study/demo only. Cut-and-paste as Batch script (file with .BAT
extension). Lines that don't begin with 2 spaces have wrapped by mistake.
============Screen capture Windows 95
C:\WORK>demo3.bat file3.txt
C:\WORK>
C:\WORK>debug file3.txt
-rcx
CX 0010
:
-d100 l10
0F19:0100 1B 20 6F 74 68 65 72 20-74 65 78 74 20 3E 0D 0A . other text >..
-q
C:\WORK>
============End screen capture
(c) A CLS curiosity
You can place the ESCape character (followed by [2J characters) in a file
by redirecting the output of the CLS (clear screen command). If you do,
the screen isn't cleared, but the ANSI sequence for clearing it (ESC[2J)
is redirected to the output file. Once again, a DEBUG byte listing shows
the characters redirected
============Screen capture Windows 95
C:\WORK>cls>file4.txt
C:\WORK>debug file4.txt
-d100 l4
0F19:0100 1B 5B 32 4A .[2J
-q
C:\WORK>
============End screen capture
--
William Allen
Note 1
WSH (Windows Script Host) is available as a free downloadable
add-on for the tiny proportion of Windows machines (mostly older
Windows 95 ones) which don't already have it installed in one
version or another. Current version (around 5.6-ish) will install
on Windows 95 machines as well as Windows 98 and ME. Documentation
is included as HTML help file, and includes cut-and-pastable syntax
examples in VBScript and JScript for all features.
Windows Script Host main page for information and downloads:
http://msdn.microsoft.com/scripting/
By default, WSH installs CSCRIPT.EXE which is a Batch file
interface allowing all WSH functionality to be run from a normal
DOS-style Batch file. This interface provides Windows machines
with a massive extension to the traditional Batch functionality.
For a readable batch line,
EKKO 27 > filename.txr
will create a one-character file containing ASCII Escape. EKKO via sig
line 3 below.
--
© John Stockton, Surrey, UK. j...@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.