"Here-document" trickery

samintz

Scott Mintz
May 20, 2008
1,312
11
Solon, OH, USA
#1
The here-document redirection is not limited to external applications. I've used it to do a couple of nifty things that keep my scripts a little cleaner (I think).

Copy a group of files
Code:
(copy /y @con: .) << endtext
file1
file2
file3
file4
file5
endtext
Display a bunch of variables
Code:
(do var in @con (echo %var = %[%var])) << endtext
_4ver
_batch
_batchline
_batchname
_batchtype
_cmdline
_cmdproc
_cmdspec
_winuser
_winver
_wow64
_xpixels
_year
_ypixels
endtext
 
Likes: epement

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,721
47
Albuquerque, NM
prospero.unm.edu
#3
I don't believe it's documented, but TYPE can read from stdin. So you can use here-documents with TYPE:

Code:
@echo off

type <<- endtext
   Shell version = %_4ver
   Command line = %_cmdline
   Command shell name = %_cmdproc
   Command shell filename = %_cmdspec
   User name = %_winuser
   Windows version = %_winver
endtext
And you can also put difficult characters like less-than, greater-than, and ampersands in here-documents, which makes them a really simply way to output HTML....
 
Nov 2, 2008
194
1
#7
You do know that EXTPROC works with batch files and rexx scripts too. You store the data file in a rexx script, and the main command file in the batch file. This is 'units.cmd', you type 'units mks' or something, and it produces a neatly presented table of units. Everything in the batch (including extproc), is read by Extproc. Most of it is formatting. It presents a neatly formatted table, partly controlled by the two rexx scripts.

I had a batch file that produced a fully-formatted word document, complete with letterhead, based pretty much on this sort of thing. For the head and tail, you just have some sort of command like ~head and ~tail to do this.

Code:
extproc systemx.rex
%  , indent 1
%  ,, indent 2
%  ; symbol
%  / full line printed
%  # k4 formula
%  unit profile
%  * change the from formula.
~$t (1$X1 = $x)
/
* from = '*'
/Conventional values
, Specific gravity (spig) ; gcc # si(700,3); kg/m3
, gravity ; g # si(9,, 9.80665) ; m/s2
, Joule constant ; j # si(19.9,, 4186.8); K/kg K
, Atmosphere ; atm # si(720,, 101325); Pa
, Solar constant; # si(730,,1361); W/m2
Code:
MKS (1E1 = 10)

Conventional values
  Specific gravity (spig)  gcc  1.00000000000000 E  3 kg/m3
  gravity  g  9.80665000000000 E  0 m/s2
  Joule constant  j  3.60000000000000 E  3 K/kg K
  Atmosphere  atm  101.325000000000 E  3 Pa
  Solar constant  1.36100000000000 E  3 W/m2