tags:

views:

7681

answers:

7

How do I display and redirect output to a file. Suppose if I use dos command, dir > test.txt ,this command will redirect output to file test.txt without displaying the results. how to write a command to display the output and redirect output to a file using DOS i.e., windows command prompt, not in UNIX/LINUX.

+11  A: 

There's a Win32 port of the Unix tee command, that does exactly that. See http://unxutils.sourceforge.net/ or http://getgnuwin32.sourceforge.net/

Brian Rasmussen
Can you please provide us solution in windows not in unix..Unix tee can provide the solution , but windows cannot recognize tee command..
The link points to a Windows implementation of the Unix command tee, so it does work on Windows.
Brian Rasmussen
Yeah, voting up the answer and the comment. I'd actually prefer CygWin since it has everything but some people prefer non-DLL tools that they can pick and choose.
paxdiablo
Lots of Unix utilities are also ported by GnuWin32 project, see http://gnuwin32.sourceforge.net/.
VladV
UnxUtils was last updated in 2003; GnuWin32 is a bit more up-to-date.
quack quixote
+3  A: 

Unfortunately there is no such thing.

MS-DOS applications (and Windows console applications, too) only have a single output handle. (Well, there are two STDOUT, STDERR but it doesn't matter here) The > redirects the output normally written to the console handle to a file handle.

If you want to have some kind of multiplexing you have to use an external application which you can divert the output to. This application then can write to a file and to the console again.

DR
+3  A: 

A simple C# console application would do the trick:

using System;
using System.Collections.Generic;
using System.IO;

namespace CopyToFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            var buffer = new char[100];
            var outputs = new List<TextWriter>();

            foreach (var file in args)
                outputs.Add(new StreamWriter(file));

            outputs.Add(Console.Out);

            int bytesRead;
            do
            {
                bytesRead = Console.In.ReadBlock(buffer, 0, buffer.Length);
                outputs.ForEach(o => o.Write(buffer, 0, bytesRead));
            } while (bytesRead == buffer.Length);

            outputs.ForEach(o => o.Close());
        }
    }
}

To use this you just pipe the source command into the program and provide the path of any files you want to duplicate the output to. For example:

dir | CopyToFiles files1.txt files2.txt

Will display the results of dir as well as store the results in both files1.txt and files2.txt.

Note that there isn't much (anything!) in the way of error handling above, and supporting multiple files may not actually be required.

Richard
Hmm, download tee/cygwin for nothing or buy MSVS with my hard-earned cash for a piddly little program like that? That's a tough one :-)
paxdiablo
You don't need visual studio to compile that, the commandline tools are actually free. just google ".net sdk download" for the link (the direct link seems to change around but google always seems to work).
Kris
Visual Studio Express is free as well, but I would still just use tee for this.
Brian Rasmussen
Home-made tee implementation? And have to learn C#? Use tee.. really.
Jay
+3  A: 

I agree with Brian Rasmussen, the unxutils port is the easiest way to do this. In the Batch Files section of his Scripting Pages Rob van der Woude provides a wealth of information on the use MS-DOS and CMD commands. I thought he might have a native solution to your problem and after digging around there I found TEE.BAT, which appears to be just that, an MS-DOS batch language implementation of tee. It is a pretty complex-looking batch file and my inclination would still be to use the unxutils port.

Steve Crane
That tee.bat thing looks nice. Hope OP checks this out.
Jay
Note that using TEE.BAT will output after the command has completed, just like the "dir > a.txt | type a.txt" example posted nearby.
adzm
A: 

Following helps if you want something really seen on the screen - even if the batch file was redirected to a file. The device CON maybe used also if redirected to a file

Example:

ECHO first line on normal stdout. maybe redirected
ECHO second line on normal stdout again. maybe redirected
ECHO third line is to ask the user. not redirected  >CON
ECHO fourth line on normal stdout again. maybe redirected

Also see good redirection description: http://www.p-dd.com/chapter7-page14.html

+5  A: 

Hello, I was able to find a solution/workaround of redirecting output to a file and to the console:

dir > a.txt | type a.txt

where dir is the command which output needs to be redirected, a.txt a file where to store output.

tori3852
This satisfies the answer, but outputs the data after the dir command has completed rather than as the data is produced.
Leigh Riffel
A: 

How do I display and redirect output to a file. Suppose if I use dos command, dir > test.txt ,this command will redirect output to file test.txt without displaying the results. how to write a command to display the output and redirect output to a file using DOS i.e., windows command prompt, not in UNIX/LINUX.

You may find these commands in biterscripting ( http://www.biterscripting.com ) useful.

var str output
lf > $output
echo $output                            # Will show output on screen.
echo $output > "test.txt"               # Will write output to file test.txt.
system start "test.txt"                 # Will open file test.txt for viewing/editing.
P M