home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / msdos / programm / 8671 < prev    next >
Encoding:
Internet Message Format  |  1992-08-20  |  3.3 KB

  1. Xref: sparky comp.os.msdos.programmer:8671 comp.lang.pascal:4935
  2. Path: sparky!uunet!gumby!wupost!waikato.ac.nz!canterbury.ac.nz!phys169
  3. Newsgroups: comp.os.msdos.programmer,comp.lang.pascal
  4. Subject: Re: How can I redirect STDERR into a file ?
  5. Message-ID: <1992Aug21.113845.448@csc.canterbury.ac.nz>
  6. From: phys169@csc.canterbury.ac.nz
  7. Date: 21 Aug 92 11:38:44 +1200
  8. References: <4117@bimacs.BITNET>
  9. Organization: University of Canterbury, Christchurch, New Zealand
  10. Lines: 91
  11.  
  12. In article <4117@bimacs.BITNET>, tamari@bimacs.BITNET (tamari zvi) writes:
  13. >   I wrote a program in Turbo Pascal that executes dos command and redirect it's
  14. >   output into a file.
  15. >   However, when an error accours the error message appear on the screen rather
  16. >   than find it's way into the output file.
  17. >   So, the question is how can I redirect these error messages into a file ?
  18. >   (it does'nt have to be the output file.
  19.  
  20. You can redirect any of the standard handles by duplicating the file handle,
  21. closing the original file, openning it again using a different file, and
  22. putting things back the way they were after executing the DOS commands.
  23.  
  24. There does seem to a be problem in Turbo Pascal, though, if you redirect a
  25. standard handle then exit the program... Turbo's wrap-up code seems to restore
  26. things automatically, undoing the redirection.
  27.  
  28. Here's a bit of Turbo Pascal to do something like what you ask...
  29.  
  30. program TryRedirect; {redirect StdErr during execution of a command}
  31. uses DOS;
  32. const
  33.   StandardInputHandle = 0;
  34.   StandardOutputHandle= 1;
  35.   StandardErrorHandle = 2;
  36.   StandardPrinterHandle=4;
  37.   StandardAuxHandle   = 3;
  38.  
  39. var RedirectionFile : text;
  40.     OldHandle       : word;
  41.     reg             : registers;
  42.     st,
  43.     param1,
  44.     param2          : string;
  45.  
  46. procedure WriteToStandardFile(Handle : word; st : string);
  47. begin
  48. with reg do begin
  49.             AH:=$40; BX:=handle; CX:=length(st);
  50.             DS:=seg(st); DX:=ofs(st[1]);
  51.             MsDos(reg);
  52.             if odd(Flags) then DosError:=AX else DosError:=0;
  53.             end;
  54. end;
  55.  
  56.  
  57. function RedirectHandle(HandleToRedirect,NewFileHandle : word) : word;
  58. begin
  59. with reg do
  60.      begin
  61.      AH:=$45;
  62.      BX:=HandleToRedirect;
  63.      MsDos(reg);   {Duplicate a file handle}
  64.      RedirectHandle:=AX; {Return the temporary copy of the original handle}
  65.      AH:=$46;
  66.      CX:=HandleToRedirect;
  67.      BX:=NewFileHandle;
  68.      if BX<>CX then MsDos(reg); {MSDOS might hang if BX=CX!}
  69.      end;
  70. end;
  71.  
  72. procedure UnRedirect(HandleToRedirect,OldFileHandle : word);
  73. begin
  74. with reg do
  75.      begin
  76.      AH:=$46;  {force duplicate handle}
  77.      CX:=HandleToRedirect;
  78.      BX:=OldFileHandle;
  79.      if BX<>CX then MsDos(reg); {MSDOS might hang if BX=CX!}
  80.      AH:=$3E;  {close a handle}
  81.      BX:=OldFileHandle;
  82.      MsDos(reg);
  83.      end;
  84. end;
  85.  
  86.  
  87. begin
  88. writeln('Redirection of stderr to a file (LPT1 in this demo)');
  89. writeToStandardFile(StandardErrorHandle,' This was teh StdErr file...'^M^J);
  90. assign(RedirectionFile,'LPT1');
  91. rewrite(RedirectionFile);
  92. writeln(RedirectionFile,'This is the file to get the redirected output...');
  93. OldHandle:=Redirect(StandardErrorHandle,TextRec(RedirectionFile).handle);
  94. close(RedirectionFile);
  95. WriteToStandardFile(StandardErrorHandle,'This goes to the standard error file');
  96. exec('c:\COMMAND.COM','/C something');
  97. UnRedirect(StandardErrorHandle,OldHandle);
  98. end.
  99.  
  100. Hope this helps,
  101. Mark Aitchison, University of Canterbury, New Zealand.
  102.