Writing the same output to multiple logs

为君一笑 提交于 2019-12-25 05:06:41

问题


Is it possible to write the same output to more than one file in Batch? My reason for wanting to do this is because I have a large batch script that produces a very detailed log. This is fine as it is, but i want to also output a trimmed back version of the log with a lot less detail in it. The Batch cannot be run multiple times either.

Say for instance I have a simple batch:

   Echo This is a Batch Script >> Path\File1 & Path\File2
   osql -S%SERVERNAME% -E -d%DATABASENAME% -Q%SQL% >> Path\File1

Appreciate any help.


回答1:


Maybe you can use the tee command from Unix tools. Downloadable for free from here. Think of it like a "T" that a plumber might put in a pipe to send water two ways.

osql -S%SERVERNAME% -E -d%DATABASENAME% -Q%SQL% | tee file1 file2 file3

Have a look at some examples as I am not entirely sure what your full processing requirement is, see here.

If you want to do some processing on one stream you can do this:

osql -S%SERVERNAME% -E -d%DATABASENAME% -Q%SQL% | tee unfiltered.txt | FINDSTR /v "UglyStuff" > filtered.txt



回答2:


Second answer, because it is different...

You could use some VBScript, like this to send your osql output to both stdout and stderr and then handle the two separately. This saves you needing to install any Unix tools.

Save this as tee.vbs

REM ############################################################################
REM File: tee.vbs
REM Author: Mark Setchell
REM I don't need any Unix purists to tell me it is not functionally idential to
REM the Unix 'tee' command, please. It does a job - that's all. And I also know
REM there is no error checking. It illustrates a technique.
REM ############################################################################
Set fso = CreateObject ("Scripting.FileSystemObject") 
Set stdout = fso.GetStandardStream (1) 
Set stderr = fso.GetStandardStream (2) 

Do While Not WScript.StdIn.AtEndOfStream
   REM Read in next line of input
   Line  = WScript.StdIn.ReadLine()
   stdout.WriteLine(Line)
   stderr.WriteLine(Line)
Loop

Then run your osql like this:

osql -S%SERVERNAME% -E -d%DATABASENAME% -Q%SQL% | cscript /nologo tee.vbs 2> unfiltered.txt | FINDSTR "goodstuff" > filtered.txt

Basically, whatever the tee.vbs script writes to stderr gets redirected to wherever 2> points, and whatever tee.vbs writes to stdout goes into the FINDSTR command.

Ideally, you could put your filtering inside the tee.vbs file for maximum flexibility.



来源:https://stackoverflow.com/questions/22323835/writing-the-same-output-to-multiple-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!