Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can you use redirection to two outputs?

16 views
Skip to first unread message

Kevin B

unread,
May 1, 2005, 1:52:21 AM5/1/05
to
Hello everyone,
 
I was wondering if I can send the output of a command to both the command window and to a log file?
 
I am currently sending the output to a log file (call c:\Data\DiskMaintenance.bat >%BACKUP_LOG% 2>&1) but it would be nice to see what is happening when I look at the command window (it is currently blank).
 
 
Thanks in advance for your help!
 
 
Kevin
 
 

Timo Salmi

unread,
May 1, 2005, 2:01:29 AM5/1/05
to
Kevin B wrote:
> I was wondering if I can send the output of a command to both the
> command window and to a log file?

On option is to use the UNIX-like tee.exe from
3365706 Apr 15 2003 ftp://garbo.uwasa.fi/win95/unix/UnxUtils.zip
UnxUtils.zip GNU utilities for native Win32

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Message has been deleted

William Allen

unread,
May 1, 2005, 3:08:44 AM5/1/05
to
"Kevin B" wrote in message

> Hello everyone,
> I was wondering if I can send the output of a command to
> both the command window and to a log file?
...snip

Yes. Note that pipes in Windows NT/2000/XP operate to memory,
and don't wait for the completion of the input-side process. This
means you can pipe the output of your Batch file to a T-filter created
in WSH with a simple VBScript file. Create the following T-filter in VBS:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
Set Args=WScript.Arguments
LogFile=Args(0)
Set fso = CreateObject("Scripting.FileSystemObject")
Set LogFile= fso.CreateTextFile(Args(0))

Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine
StdOut.WriteLine str
LogFile.WriteLine str
Loop

LogFile.Close

====End cut-and-paste (omit this line)
For study/demo use. Cut-and-paste as VBScript (.VBS) text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

With that file cut-and-paste as, say TFILTER.VBS, run your
Batch file with a pipe to the filter executed with CSCRIPT and
add the logfile name you require as a parameter.

Something like this:

MyBatchFile.bat | cscript//nologo TFILTER.VBS MyLogFile.txt

The output of the Batch file is displayed line-by-line and
simultaneously captured to MyLogFile.txt line-by-line.

If you change the VBS line:
LogFile.WriteLine str
to
LogFile.WriteLine "Line " & (StdIn.Line - 1) & ": " & str
this will number all the lines in the log (but not on screen).

Note: You must execute the TFILTER.VBS with CSCRIPT and
not WSCRIPT (STDIN and STDOUT are not available via WSCRIPT).

This technique avoids the use of third-party utilities.

For full documentation on using VBS in Batch files:
http://msdn.microsoft.com/scripting/

--
William Allen
Free interactive Batch Course http://www.allenware.com/icsw/icswidx.htm
Batch Reference with examples http://www.allenware.com/icsw/icswref.htm
Header email is rarely checked. Contact us at http://www.allenware.com/


William Allen

unread,
May 1, 2005, 3:11:32 AM5/1/05
to
"Kevin B" wrote in message
> Hello everyone,
> I was wondering if I can send the output of a command to
> both the command window and to a log file?

Timo Salmi

unread,
May 1, 2005, 4:30:37 AM5/1/05
to
William Allen wrote:
> "Kevin B" wrote in message
>> I was wondering if I can send the output of a command to both
>> the command window and to a log file?

> in WSH with a simple VBScript file. Create the following T-filter
> in VBS:

> With that file cut-and-paste as, say TFILTER.VBS, run your

> MyBatchFile.bat | cscript//nologo TFILTER.VBS MyLogFile.txt

That is very useful. It also is nice in the sense that it is on the
borderline between using third-party utilities. Here the user can
prepare the utility him/herself, or can have a batch to do it.

Anyway, suggestions and observations: Maybe VTEE.VBS would, for
obvious reasons, be an optimal name for your VBS script.

Errors behave differently from running the original batch straight.
Not a flaw, but a feature. Consider a recent example of CMDFAQ.CMD
containing

@echo off & setlocal enableextensions
if not [%1]==[] echo %1
if not [%1]==[] echo %~1
set par1=%1
if defined par1 echo %1
if not "%1"=="" echo %1
endlocal & goto :EOF

C:\_D\TEST>cmdfaq "a b"
"a b"
a b
"a b"
b""=="" was unexpected at this time.

However:
C:\_D\TEST>cmdfaq "a b" | cscript//nologo VTEE.VBS MyLogFile.txt
b""=="" was unexpected at this time.
"a b"
a b
"a b"

and
C:\_D\TEST>type MyLogFile.txt
"a b"
a b
"a b"

Timo Salmi

unread,
May 1, 2005, 5:07:58 AM5/1/05
to
DRAFT

94) Can I send the script's output both to the screen and a log file?

One option is to use the UNIX-like tee.exe from


GNU utilities for native Win32

ftp://garbo.uwasa.fi/win95/unix/UnxUtils.zip

To show an example apply that on the CMDFAQ.CMD script containing


@echo off & setlocal enableextensions
if not [%1]==[] echo %1
if not [%1]==[] echo %~1

if not "%1"=="" echo %1
endlocal & goto :EOF

The output will be
C:\_D\TEST>cmdfaq "a b" | tee MyLog.txt


"a b"
a b
b""=="" was unexpected at this time.

C:\_D\TEST>type MyLog.txt
"a b"
a b
The error message goes to stdout only.

Another option is to use a screen capture program. A third one is to
write a VBS cscript to do the "tee" (see the reference below). [The
reference is to William's solution and may not be immediately at Google.]

References/Comments:
http://www.google.com/groups?selm=42748127$0$558$ed2e...@ptn-nntp-reader04.plus.net

William Allen

unread,
May 1, 2005, 7:33:08 AM5/1/05
to
"Timo Salmi" wrote in message

> William Allen wrote:
> > "Kevin B" wrote in message
> >> I was wondering if I can send the output of a command to both
> >> the command window and to a log file?
>
> > in WSH with a simple VBScript file. Create the following T-filter
> > in VBS:
>
> > With that file cut-and-paste as, say TFILTER.VBS, run your
>
> > MyBatchFile.bat | cscript//nologo TFILTER.VBS MyLogFile.txt
>
> That is very useful. It also is nice in the sense that it is on the
> borderline between using third-party utilities. Here the user can
> prepare the utility him/herself, or can have a batch to do it.
>
> Anyway, suggestions and observations: Maybe VTEE.VBS would, for
> obvious reasons, be an optimal name for your VBS script.
...snip

In that case, yes. But I named it as I did because I regard it as a
particular case of the general filter technique. Basic pass-through is:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut

Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine
StdOut.WriteLine str

Loop

====End cut-and-paste (omit this line)

For study/demo use. Cut-and-paste as VBscript (.VBS) text file.

Once you have that pass-through filter outline (it does nothing
but capture the string and pass it through), you can do
many things, as particular cases of filtering. For example:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut

Do While Not StdIn.AtEndOfStream
str = StdIn.ReadLine

StdOut.WriteLine Right("000" & Len(str),2) & ":" & str
Loop


====End cut-and-paste (omit this line)

For study/demo use. Cut-and-paste as SLengthFilter.VBS text file.

Used as a filter, that prefixes every line with its length (lead-zero packed):

============Screen capture Windows 2000 simulated in Win95
C:\WORK>shift /?| cscript//nologo SlengthFilter.vbs
63:Changes the position of replaceable parameters in a batch file.
00:
10:SHIFT [/n]
00:
60:If Command Extensions are enabled the SHIFT command supports
62:the /n switch which tells the command to start shifting at the
66:nth argument, where n may be between zero and eight. For example:
00:
12: SHIFT /2
00:
68:would shift %3 to %2, %4 to %3, etc. and leave %0 and %1 unaffected.

C:\WORK>
============End screen capture

Probably the best thing to do is to keep in mind the pass-through
outline, and adapt it as required to a particular task.

Timo Salmi

unread,
May 1, 2005, 10:36:07 AM5/1/05
to
William Allen wrote:
> In that case, yes. But I named it as I did because I regard it as
> a particular case of the general filter technique. Basic
> pass-through is:
(snip)

> Once you have that pass-through filter outline (it does nothing
> but capture the string and pass it through), you can do many
> things, as particular cases of filtering. For example:

> ====Begin cut-and-paste (omit this line) Set StdIn = WScript.StdIn


> Set StdOut = WScript.StdOut Do While Not StdIn.AtEndOfStream str
> = StdIn.ReadLine StdOut.WriteLine Right("000" & Len(str),2) & ":"
> & str Loop ====End cut-and-paste (omit this line)

Yes, indeed. It is very much the same "stream" philosophy as SED/AWK
are used for. In fact, so much so that they are interchangeable,
probably for most purposes. That's good. It gives the user additional
options to choose from.

(My apologies for Mozilla's invasive wrapping of the quoted text.)

0 new messages