5.2.4 Streams and files
All generator output is appended to the current output stream.
By default, the stream just goes to a window, whose result will be shown to the
user at the end of the generation. It is also possible to direct the output to
files (see Section
6.4) or variables
(Section
6.5). Temporary streams are
also used by some commands to form their arguments, as we will see
shortly.
In our example case, we can simply forward the output to a
file by using the
filename...write...close
command:
01 SpecSheet()
02 _translators()
03 filename id%file '.txt' write
04 foreach >Watch
05 {
.
.
.
22 }
23 close
Everything between
filename and
write goes to make up the name of the
file; everything between write and
close is written to that file. The name
of the file here in line 3 is thus the value of Graph’s identifier
property (i.e. ‘WatchModels’), translated to be suitable for a
filename with the %file translator, and
followed by ‘.txt’. After the
write command, all output will be
collected to go into this file until the
close command at the end of the
generator (in line 23), which closes the file stream and actually performs the
write to disk.
The example above collects the spec sheets for all Watch
models into one file. In order to have a separate file for each model, we can
simply move the
filename...write...close command inside
the main foreach loop:
01 SpecSheet()
02 _translators()
03 foreach >Watch
04 { filename id%file '.txt' write
.
.
.
22 close
23 }
Instead of one ‘WatchModels.txt’ file we
will now get ‘Ace.txt’, ‘Delicia.txt, ‘Sporty.txt’
etc. All files are generated into the default directory but we can also prefix
the filename with directory information, if needed (the
sep command outputs the
platform’s directory separator,
‘\’ or
‘/’):
filename 'reports' sep id%file '.txt' write
MetaEdit+
also provides a pre-defined subgenerator that outputs the default relative path
for the reports directory created by the installer. Using this subgenerator, the
above piece of code would look like this:
filename
subreport '_default directory' run
id%file '.txt'
write
The filename can thus be constructed using any MERL
commands between filename and
write: all their output goes to build
up the filename. You can also override the existing ‘_default
directory’ subgenerator or write a new one that suits your purposes.
Alternatively you can store a directory into a variable for further
reference:
$myDir = 'reports\'
filename $myDir id%file '.txt' write