954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

.bat - Direct stdout to console AND file

Hi guys,

Basically what I want to do is run a series of commands in my batch file, and display stdout to screen as well as saving it in a file. So to set the context..

::To output to helloworld.txt
Echo hello world! > helloworld.txt

::To output to console
Echo hello world! > CON


What I’d like to do is have the output of a single command directed to both. As a workaround I’ve got something which will output to a file, and then copy the contents of the file to console.

::Output to file then copy file contents to console
echo Hello World! >test.txt
copy test.txt con > nul


This isn’t ideal since I’d like for the user to see the activity in the console as the batch file is executing commands, rather than in a big batch at the end.

Cheers,

Anthony

Slade
Practically a Master Poster
633 posts since Mar 2004
Reputation Points: 115
Solved Threads: 7
 

In unix-land, this would be a doddle.
http://www.rootr.net/man/man/tee/1

If you have cygwin installed, then you can just use the 'tee' provided in your cygwin bin directory.

Though if you search long enough, you might find a native Win32 implementation as well.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Hi Salem,

Thanks for your reply. The problem is that it kind of has to be native to windows since these are scripts that will be run on other peoples machines who may not have cygwin. I'll keep digging around and see if I can figure this one out. Will keep you all posted.

Actually on this note (but on a tanget to the topic), I have another script which I use to generate C# code documentation using doxygen. If someone doesn't have doxygen clearly this script will not work. I figure one potential way around it though might be to package the doxygen exe with the scripts (I guess this would work for the tee suggestion as well) - but I'm not sure licensing would really permit that. I haven't looked into it myself in depth yet, but would anybody know off hand if the GNU General Public License allows for this?

Permission to use, copy, modify, and distribute this software and its documentation under the terms of the GNU General Public License is hereby granted.

To me that sounds like a yes, but I could be mistaken.

Cheers,

Anthony

Slade
Practically a Master Poster
633 posts since Mar 2004
Reputation Points: 115
Solved Threads: 7
 

Hey There,

Here's a link to a win32 tee emulator:

http://www.csc.calpoly.edu/~bfriesen/software/console.shtml

Otherwise, if you can somehow manage to do this with robocopy, it's the only dos command I know that has a /TEE option

Best of luck,

Mike

eggi
Posting Pro in Training
400 posts since Oct 2007
Reputation Points: 102
Solved Threads: 47
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You