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.
views:
7681answers:
7There's a Win32 port of the Unix tee
command, that does exactly that. See http://unxutils.sourceforge.net/ or http://getgnuwin32.sourceforge.net/
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.
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.
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.
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
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.
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.