home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.os.msdos.programmer:8671 comp.lang.pascal:4935
- Path: sparky!uunet!gumby!wupost!waikato.ac.nz!canterbury.ac.nz!phys169
- Newsgroups: comp.os.msdos.programmer,comp.lang.pascal
- Subject: Re: How can I redirect STDERR into a file ?
- Message-ID: <1992Aug21.113845.448@csc.canterbury.ac.nz>
- From: phys169@csc.canterbury.ac.nz
- Date: 21 Aug 92 11:38:44 +1200
- References: <4117@bimacs.BITNET>
- Organization: University of Canterbury, Christchurch, New Zealand
- Lines: 91
-
- In article <4117@bimacs.BITNET>, tamari@bimacs.BITNET (tamari zvi) writes:
- > I wrote a program in Turbo Pascal that executes dos command and redirect it's
- > output into a file.
- > However, when an error accours the error message appear on the screen rather
- > than find it's way into the output file.
- >
- > So, the question is how can I redirect these error messages into a file ?
- > (it does'nt have to be the output file.
-
- You can redirect any of the standard handles by duplicating the file handle,
- closing the original file, openning it again using a different file, and
- putting things back the way they were after executing the DOS commands.
-
- There does seem to a be problem in Turbo Pascal, though, if you redirect a
- standard handle then exit the program... Turbo's wrap-up code seems to restore
- things automatically, undoing the redirection.
-
- Here's a bit of Turbo Pascal to do something like what you ask...
-
- program TryRedirect; {redirect StdErr during execution of a command}
- uses DOS;
- const
- StandardInputHandle = 0;
- StandardOutputHandle= 1;
- StandardErrorHandle = 2;
- StandardPrinterHandle=4;
- StandardAuxHandle = 3;
-
- var RedirectionFile : text;
- OldHandle : word;
- reg : registers;
- st,
- param1,
- param2 : string;
-
- procedure WriteToStandardFile(Handle : word; st : string);
- begin
- with reg do begin
- AH:=$40; BX:=handle; CX:=length(st);
- DS:=seg(st); DX:=ofs(st[1]);
- MsDos(reg);
- if odd(Flags) then DosError:=AX else DosError:=0;
- end;
- end;
-
-
- function RedirectHandle(HandleToRedirect,NewFileHandle : word) : word;
- begin
- with reg do
- begin
- AH:=$45;
- BX:=HandleToRedirect;
- MsDos(reg); {Duplicate a file handle}
- RedirectHandle:=AX; {Return the temporary copy of the original handle}
- AH:=$46;
- CX:=HandleToRedirect;
- BX:=NewFileHandle;
- if BX<>CX then MsDos(reg); {MSDOS might hang if BX=CX!}
- end;
- end;
-
- procedure UnRedirect(HandleToRedirect,OldFileHandle : word);
- begin
- with reg do
- begin
- AH:=$46; {force duplicate handle}
- CX:=HandleToRedirect;
- BX:=OldFileHandle;
- if BX<>CX then MsDos(reg); {MSDOS might hang if BX=CX!}
- AH:=$3E; {close a handle}
- BX:=OldFileHandle;
- MsDos(reg);
- end;
- end;
-
-
- begin
- writeln('Redirection of stderr to a file (LPT1 in this demo)');
- writeToStandardFile(StandardErrorHandle,' This was teh StdErr file...'^M^J);
- assign(RedirectionFile,'LPT1');
- rewrite(RedirectionFile);
- writeln(RedirectionFile,'This is the file to get the redirected output...');
- OldHandle:=Redirect(StandardErrorHandle,TextRec(RedirectionFile).handle);
- close(RedirectionFile);
- WriteToStandardFile(StandardErrorHandle,'This goes to the standard error file');
- exec('c:\COMMAND.COM','/C something');
- UnRedirect(StandardErrorHandle,OldHandle);
- end.
-
- Hope this helps,
- Mark Aitchison, University of Canterbury, New Zealand.
-