[comp.lang.pascal] Redirecting I/O for .BAT files?

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/14/91)

How does one redirect output for a .BAT file?  Using plain old COMMAND.COM:

>DIR > FOO.OUT			{ Works as expected }

>LS > FOO.OUT			{ fails! }

where LS is a .BAT file that simply has:

@echo off

dir %1

I'm trying to get  my  shell to obey  I/O  redirection in both cases but it
ain't working.   Now I  find  out that even  COMMAND.COM doesn't  obey  I/O
redirection for the output of a simple .BAT file!?!  Is  it possible  to do
this in TP?

Of course once I get the  ">" and ">>"  for .BAT  files problem solved I'll
need to address the "|" for .BAT files problem.

Thanks in advance.

s2525090@techst02.technion.ac.il (Eran Davidov) (06/17/91)

If you want to Redirect the IO from a batch file, there must first of
all be an output to redirect.
when you used the "@echo off", you made sure nothing was to be printed to the
screen. therefore, nothing was redirected. try the following batch file:
--- start of batch
DIR
--- end of batch

now, when you use it this way: FOO > OUT.DAT
the output will be redirected to a file, instead of appearing on your screen.

			Hope it helps, Eran

s9100202@giaea.gi.oz (Lee Hollingworth) (06/18/91)

In article <27213@adm.brl.mil> s2525090@techst02.technion.ac.il (Eran Davidov) writes:
>If you want to Redirect the IO from a batch file, there must first of
>all be an output to redirect.
>when you used the "@echo off", you made sure nothing was to be printed to the
>screen. therefore, nothing was redirected. try the following batch file:
>--- start of batch
>DIR
>--- end of batch
>
>now, when you use it this way: FOO > OUT.DAT
>the output will be redirected to a file, instead of appearing on your screen.
>
>			Hope it helps, Eran

Have you actually tried this?

My understanding is that using your example, DOS would create a file OUT.DAT
but the size of that file would be 0.

To redirect output from a batch file, you must use the Command command.

command /c FOO > OUT.DAT

This would redirect the output from FOO.BAT to OUT.DAT.


*******************************************
Lee Hollingworth  s9100202@giae.oz.au
Remember: Change is here to stay!
*******************************************

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/19/91)

>/ hpcupt3:comp.lang.pascal / s2525090@techst02.technion.ac.il (Eran Davidov) /  4:04 pm  Jun 16, 1991 /
>If you want to Redirect the IO from a batch file, there must first of
>all be an output to redirect.
>when you used the "@echo off", you made sure nothing was to be printed to the
>screen. therefore, nothing was redirected. try the following batch file:
>--- start of batch
>DIR
>--- end of batch
>
>now, when you use it this way: FOO > OUT.DAT
>the output will be redirected to a file, instead of appearing on your screen.
>
>			Hope it helps, Eran

This is totally untested and blantly false.  "@echo off" does no such thing
and the example .BAT file just does a DIR to the screen (and creates a zero
length OUT.DAT file) when FOO > OUT.DAT is executed.

I guess I'll just have  to re-implement the wheel, open  the  .BAT file and
execute the commands myself, redirecting output as I go.  This is  gonna be
a pain, unless someone knows a better answer....

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/19/91)

>/ hpcupt3:comp.lang.pascal / s9100202@giaea.gi.oz (Lee Hollingworth) / 11:05 pm  Jun 17, 1991 /

>To redirect output from a batch file, you must use the Command command.
>
>command /c FOO > OUT.DAT
>
>This would redirect the output from FOO.BAT to OUT.DAT.

I was surprized that this does indeed work.  Care to relate that in TP terms
now?  I thought that it would be easy, simply run a COMMAND.COM to run yet
another COMMAND.COM with the /C flag and the output from a .BAT file would be
redirected but it doesn't work:

{$M 16384,0,0}

Program Foo;

Uses DOS;

Begin { Foo }
  Exec ('D:\COMMAND.COM', 'D:\COMMAND.COM /C C:\ANDY\FOO.BAT > FOO.OUT');
  Writeln ('DOSError = ', DOSError);

End. { Foo }

Where D:\COMMAND.COM is a valid COMMAND.COM and C:\ANDY\FOO.BAT contains:

DIR

When this is run I see:

Specified COMMAND search directory bad
DOSError = 0

written to the screen and in examining FOO.OUT I find simply:

DOSError = 2

Now what am I doing wrong?

dmurdoch@watstat.waterloo.edu (Duncan Murdoch) (06/20/91)

In article <45670022@hpcupt3.cup.hp.com> defaria@hpcupt3.cup.hp.com (Andy DeFaria) writes:
>
>Begin { Foo }
>  Exec ('D:\COMMAND.COM', 'D:\COMMAND.COM /C C:\ANDY\FOO.BAT > FOO.OUT');
>  Writeln ('DOSError = ', DOSError);
...
>
>When this is run I see:
>
>Specified COMMAND search directory bad
>DOSError = 0
>
>written to the screen and in examining FOO.OUT I find simply:
>
>DOSError = 2
>
>Now what am I doing wrong?

You forgot a /C at the start of the command line.  It should be

  Exec ('D:\COMMAND.COM', '/C D:\COMMAND.COM /C C:\ANDY\FOO.BAT > FOO.OUT');

The first /C tells the first command processor that what follows is a command,
and the second one tells the second processor that the batch file is a command.
I think the first command processor is the one that sees and acts on the
'> FOO.OUT', so that the second command processor already has STDOUT redirected.

A possibly simpler way to achieve the same thing is to use the FORCDUP DOS call
to redirect STDOUT into a file.  That method can also be used to redirect
STDERR, if you want to do that.

Duncan Murdoch

s9100202@giaea.gi.oz (Lee Hollingworth) (06/22/91)

In article <45670021@hpcupt3.cup.hp.com> defaria@hpcupt3.cup.hp.com (Andy DeFaria) writes:
>>/ hpcupt3:comp.lang.pascal / s2525090@techst02.technion.ac.il (Eran Davidov) /  4:04 pm  Jun 16, 1991 /

[lot's deleted..]

>This is totally untested and blantly false.  "@echo off" does no such thing
>and the example .BAT file just does a DIR to the screen (and creates a zero
>length OUT.DAT file) when FOO > OUT.DAT is executed.
>
>I guess I'll just have  to re-implement the wheel, open  the  .BAT file and
>execute the commands myself, redirecting output as I go.  This is  gonna be
>a pain, unless someone knows a better answer....


Have a look at article 4334 in comp.lang.pascal -- I've already answered this
once.

The only way to get DOS to redirect the output from a *.bat file is to do so
with the COMMAND command eg.,

command /c foo > out.dat

/******
 * Lee Hollingworth s9100202@giaea.oz.au
 ******/

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/25/91)

>/ hpcupt3:comp.lang.pascal / s9100202@giaea.gi.oz (Lee Hollingworth) /  6:05 pm  Jun 21, 1991 /
>
>Have a look at article 4334 in comp.lang.pascal -- I've already answered this
>once.
>
>The only way to get DOS to redirect the output from a *.bat file is to do so
>with the COMMAND command eg.,
>
>command /c foo > out.dat
>
>/******
> * Lee Hollingworth s9100202@giaea.oz.au
> ******/
>----------

Yeah  this works fine at  the  command line but  how do you accomplish this
programmatically with TP?

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/27/91)

>/ hpcupt3:comp.lang.pascal / Kai_Henningsen@ms.maus.de (Kai Henningsen) /  8:28 am  Jun 24, 1991 /

>Well, several comments.
>
>1. Do not use 'D:\COMMAND.COM', except if you try to call a *different* COMMAND
>from the one that's your default. Use GetEnv('COMSPEC').

In my  own version  I did.  In the  version  I  posted I  eliminated it for
brievity and so that there was no mistake as to the actual file used.

>2. You forgot to use /c for the first COMMAND.

Yeah I know.  I put it in.  Same results.

>3. You forgot to use SwapVectors.

My shell program does this.  My example didn't.  I believe I put it into my
example and tried it again without success.

>So, try it like this:
>
>var
>  CommandCom: string;
>
>  CommandCom := GetEnv('COMSPEC');
>  SwapVectors;
>  Exec (CommandCom, '/c '+CommandCom+' /C C:\ANDY\FOO.BAT > FOO.OUT');
>  SwapVectors;

OK.  I'll  try  it  again - verbatim  (plus a  Program  statement  and  the
appropriate  Begin/End's  of course).   Don't think it   will work but I'll
report it back to you.  I have  some  questions though: Did *you* try this?
Does it work for you?  If so, what  version of  DOS/TP are you using.  What
is your chip (mine's an HP Classic Vectra 286 with DOS 3.3 and TP 6.0)?

defaria@hpcupt3.cup.hp.com (Andy DeFaria) (06/28/91)

>/ hpcupt3:comp.lang.pascal / defaria@hpcupt3.cup.hp.com (Andy DeFaria) / 11:01 am  Jun 26, 1991 /

>>So, try it like this:
>>
>>var
>>  CommandCom: string;
>>
>>  CommandCom := GetEnv('COMSPEC');
>>  SwapVectors;
>>  Exec (CommandCom, '/c '+CommandCom+' /C C:\ANDY\FOO.BAT > FOO.OUT');
>>  SwapVectors;
>
>OK.  I'll  try  it  again - verbatim  (plus a  Program  statement  and  the
>appropriate  Begin/End's  of course).   Don't think it   will work but I'll
>report it back to you.  I have  some  questions though: Did *you* try this?
>Does it work for you?  If so, what  version of  DOS/TP are you using.  What
>is your chip (mine's an HP Classic Vectra 286 with DOS 3.3 and TP 6.0)?

OK I tried it again and it still fails horribly!  The disc churns for about
5 seconds when the  program is  running.  What  I get in  FOO.OUT is a zero
length file.  Again I ask: Did *you* try this?  Does it work for you?  Does
anybody have anything that really does work!