[comp.sys.ibm.pc.programmer] Redirecting output of DOS batch files

floyd@starsend.UUCP (Floyd Miller) (03/03/90)

I have a few batch files that run pipelined programs,
searching for a pattern, extracting fields and sorting.
They make life a bit easier by elliminating the need
to retype long command lines.

However, sometimes I want to run the final output through
a pager (like "more") or save the results in a file.
I try to run:  filter.bat | more
More doesn't do anything.
So I try:      filter.bat > f.out
f.out gets created with no data while the output
of filter.bat comes pouring out on my screen.

Apparently, REDIRECTING DOESN'T WORK ON BATCH FILES.

I was surprised mostly by the fact that it took up until
now for me to discover this deficiency in DOS.

I think that a program which overlays stderr onto stdout
and then invokes the batch file would allow redirection
of the batch file output.  Then you would run the batch
file as:

       redir prog.bat
  or   redir prog.bat > file.out
  or   redir prog.bat | more

I will try this.  And, if there is interest I'll post
"redir" if it works.  But is there an easier way?
Perhaps without having to use a separate program to
invoke the batch file?

*******   *****************************************
*****  ************************* Floyd Miller
***  *************** floyd@starsend.UUCP
*  ********* floyd%starsend@PRC.Unisys.com
  *** starsend!floyd@burdvax.PRC.Unisys.com
*

austin@bucsf.bu.edu (Austin H. Ziegler, III) (03/04/90)

>>>>> On 2 Mar 90 22:57:35 GMT, floyd@starsend.UUCP (Floyd Miller) said:

Floyd> I have a few batch files that run pipelined programs,
Floyd> searching for a pattern, extracting fields and sorting.
Floyd> They make life a bit easier by elliminating the need
Floyd> to retype long command lines.

Floyd> However, sometimes I want to run the final output through
Floyd> a pager (like "more") or save the results in a file.
Floyd> I try to run:  filter.bat | more
Floyd> More doesn't do anything.
Floyd> So I try:      filter.bat > f.out
Floyd> f.out gets created with no data while the output
Floyd> of filter.bat comes pouring out on my screen.

Floyd> Apparently, REDIRECTING DOESN'T WORK ON BATCH FILES.

	Not in the way that you are trying to do it.  MS-DOS sees
filter.bat | more as 
%0         %1 %2

	Thus in your batch file you would have to mark everything with
%1 and %2.  Unfortunately, you would have to have the redirection symbol
and the target in fixed positions.  The command line can handle most other
redirection that goes to stdout/stderr, but it cannot handle it in batch
files because it is trying to treat the pipe/redirection portions as
parameters.  An example might be:

FILTER.BAT
rem Show how redirection can be used in a batch file.
echo This is a test message. %1 %2
copy filter.bat nul %1 %2

Now this is a stupid example, but it shows that batch files *can* do
redirection, you have to do the redirection information by hand.

hope that helps,
austin

floyd@starsend.UUCP (Floyd Miller) (03/11/90)

I appreciate the responses both via email and followup articles.
The use of COMMAND /C certainly satisfies the problem in my
original posting: redirecting the output from a batch file.
The ensuing discussion on the use of the /C option with EXEC
and SPAWN was also interesting.

A second operation I discovered I needed was a way to redirect
the stderr output.  Unix shells provide the means to accomplish
this but apparently not COMMAND.COM.

Using the examples of EXEC to invoke COMMAND.COM from within a C
program I am writing a REDIR program which will combine stderr
with stdout so both can be redirected to the same place.  At least
that partially solves the problem.  I'm not worried right now about
redirecting them separately.  Once I'm finished I'll submit it to
C.B.I.P for posting - if I can figure out how to do that.

I found a problem with the code posted for exec'ing COMMAND.COM.  Using:

      execlp("COMMAND.COM", "/C", cmd_string, NULL);

starts up COMMAND.COM interactively and results in an error message:

      Specified MS-DOS search directory bad.

I discovered, experimentally that what is required is:

      execlp("COMMAND.COM", "COMMAND.COM", "/C", cmd_string, NULL);

so that the 1st argument passed to COMMAND.COM is its name.
I hope this helps anyone else whose had a similar problem.

*******   *****************************************
*****  ************************* Floyd Miller
***  *************** floyd@starsend.UUCP
*  ********* floyd%starsend@PRC.Unisys.com
  *** starsend!floyd@burdvax.PRC.Unisys.com
*