Computer Hope

Microsoft => Microsoft DOS => Topic started by: wlchen on August 29, 2007, 02:57:30 AM

Title: Redirecting output to both file and screen
Post by: wlchen on August 29, 2007, 02:57:30 AM
Is it possible in DOS to print the output both in a file and stdout ?
I am aware that in Linux, you may type
ls | tee ls.log
Is there anything similar in DOS command ?
Thanks for the help !
^^


Title: Re: Redirecting output to both file and screen
Post by: ghostdog74 on August 29, 2007, 03:17:37 AM
a couple of ways:
1) search for the windows port of the tee command ..you know how to right?
2) do your own, eg save the below vbscript as tee.vbs
Code: [Select]
Dim StdIn, StdOut,objFSO
Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
   outFile = objArgs(I)
Next
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(outFile,True)
Do While Not StdIn.AtEndOfStream
     str = StdIn.ReadLine
     StdOut.WriteLine "Line " & (StdIn.Line - 1) & ": " & str
     objFile.Write str & vbCrLf
Loop
objFile.Close
Set objFSO=Nothing
Set objFile=Nothing
Set objArgs=Nothing
Set StdOut=Nothing
Set StdIn=Nothing
example usage:
Code: [Select]
c:\> dir | cscript /nologo tee.vbs outputfile
3) i believe there's some ways to do it in DOS, i will just leave it to the DOS experts to guide you. In the meantime, how about this in batch :
Code: [Select]
c:\> dir
c:\> dir > outputfile
Title: Re: Redirecting output to both file and screen
Post by: contrex on August 29, 2007, 03:37:37 AM
1) search for the windows port of the tee command ..you know how to right?

It is surprising that someone with a Unix type background does not know how to type "tee port NT" into Google. If they did they would have found (among others) Rob van der Woude's tee.bat, or the excellent blip project's package which includes tee.cmd.

Mind you, we are assuming the OP actually meant "NT/W2k/XP/Vista command line" when they typed "DOS"... not always a safe assumption... maybe they will clarify?

For "real" MS-DOS there is FORK.COM, again findable with Google (it's in FORK33.ZIP)
Title: Re: Redirecting output to both file and screen
Post by: ghostdog74 on August 29, 2007, 03:43:20 AM
Quote
Mind you, we are assuming the OP actually meant "NT/W2k/XP/Vista command line" when they typed "DOS"... not always a safe assumption... maybe they will clarify?
hmm...how about another sub forum called Disk Operating System (DOS).? is it warranted?
Title: Re: Redirecting output to both file and screen
Post by: contrex on August 29, 2007, 03:53:43 AM
Well, to me there is MS-DOS and there is NT command prompt. They are different things, and I don't like to see them mixed up. Because the NT command language has many of the same commands as MS-DOS, with (sometimes, but not always) the same syntax, many people confuse them, or assume that the command window you get in 2K/XP/Vista is actually called "DOS".

I guess the problem is most acute among the "GUI generation" who are aged under about 25, who have grown up with Windows of one form or another.

I personally feel that this forum has made a rod for its own back by not sufficiently distinguishing between them.

I can imagine that answering questions that ask about "DOS" with a purely MS-DOS based answer would pretty soon ram the point home...