1.

Solve : Split output to display & log file?

Answer»

Searching all over for a way to split output to display & log file when running batch file.

An example without listing my lengthy batch file is the following:

Code: [Select]echo off
cls
xcopy c:\Data\*.* g:\*.* /s/d/y>>xCopy_Log.txt
echo. Backup Complete
pause
echo on
The above will pass what normally would be displayed to the user to the xCopy_Log.txt file and the display to the user would be not echo'd with info until the xcopy routine is completed in which point you would see

Quote

Backup Complete

Press any key to CONTINUE..
and it would exit.

Not sure if splitting an output is POSSIBLE in batch or not. Was thinking that if the output cant be split that one could display the log using say a program in C++ to display x-Many lines from the log at a time or scroll through what has already been COPIED on the display after the copying has ended such as

Code: [Select]echo off
cls
xcopy c:\Data\*.* g:\*.* /s/d/y>>xCopy_Log.txt
Display_xCopy_Log.exe
echo. Backup Complete
pause
echo on
To me it seems as their has to be an easier way to go about this to display real-time echo'd information + logging it by splitting the output somehow. But this is beyond my batch coding abilities so figured I'd post it here.

*Also to mention that if I was to have to use a C++ program to display the latest xcopy after the actual xcopy has been completed, I'd have to go with > vs >> to avoid lengthy log playbacks of prior xcopy routines logged. The reason for using appending to end >> is because my lengthy batch PROCESS currently appends to the log file.

***UPDATE***

Through different search words in Google I came across a TEE for Windows. But not sure if this is the best solution or not. Seen comments that its buggy under some conditions. http://code.google.com/p/wintee/

Would be nice to be able to do this in just batch, but can be processed in any other code type as well. Starting to think that maybe its beyond what Batch natively can do.A Tee filter is what you need. There are millions of them from the dos era and which don't work in the NT line of windows.

The SFK.EXE (swiss file knife) is free and still updated which has a tee capability - you can try that.

You might however prefer to use Robocopy which can log and write to the screen too.I have this squirreled away too - which could be used. A post by Tom, saved from Usenet.

Code: [Select]' Tee.vbs
set con = createobject("scripting.filesystemobject")_
.opentextfile("con:", 2)
do until wsh.stdin.atendofstream
s = wsh.stdin.readline
wsh.echo s : con.writeline s
loop

'In Win7, if cscript.exe is defined as the default host and the SCRIPT is found in the current directory or is named on the PATH, it can be used directly with a pipe, like so ...

' ping localhost | tee > test.txt

'Otherwise and in XP, the cscript host and script extension must be named explicitly ...

' ping localhost | cscript pathspec\tee.vbs > test.txt

' Tom LavedasTEE is the normal way of doing what you describe in *nix family OS types. There are a number of TEE implementations for Windows. That "wintee" one that you have found is a single-person project, and I personally would recommend one of the more robust open-source efforts such as the ones contained in:

The Unxutils package http://unxutils.sourceforge.net/

The GNUwin32 package http://gnuwin32.sourceforge.net/packages/coreutils.htm




Here's a batch file encompassing the tee filter. I think it appends or creates the log file - another file by Tom Lavedas.

Code: [Select] @echo off
> TFILTER.VBS echo. sLogName="Logfile.log" ' default name
>>TFILTER.VBS echo. if WSH.Arguments.Count^>0 then sLogName=WSH.Arguments(0)
>>TFILTER.VBS echo. with CreateObject("Scripting.FileSystemObject")_
>>TFILTER.VBS echo. .OpenTextFile(sLogName, 8, true)
>>TFILTER.VBS echo. .WriteLine Now
>>TFILTER.VBS echo. Do Until WSH.StdIn.AtEndOfStream
>>TFILTER.VBS echo. str = WSH.StdIn.ReadLine
>>TFILTER.VBS echo. wsh.echo str : .WriteLine str
>>TFILTER.VBS echo. Loop
>>TFILTER.VBS echo. end with ' FSO

(echo === Started ====
echo your commands here
copy *.bat "c:\folder" /y
echo ====text here=== )|cscript //nologo TFILTER.VBS MyLogFile.txt

del TFILTER.VBS


Why not just put it in a FOR loop?

Code: [Select]@echo off
for /f "delims=" %%a in ('xcopy c:\Data\*.* g:\*.* /s/d/y') do echo %%a & echo %%a >> xCopy_Log.txtThanks everyone, going to try out what Foxidrive suggested with vb script and tee, & what Helpmeh suggested which might be a way to do it in batch without tee.


Discussion

No Comment Found