DOS命令重定向到文件截断输出

时间:2012-07-20 21:02:47

标签: redirect buffer dos

我有一个命令行工具,通常会转储大约200多行输出。我正在寻找出现在此输出结尾的文字。当我将输出重定向到文件时:

C:\> somecommand > results.txt 2>&1

...只有前100行左右的输出显示在此文件中。同样,如果我将输出管道输入类似于“发现”的内容,接收程序将无法在大约第100行之后查找或操作任何文本。

shell的屏幕缓冲区大小设置似乎对可捕获的行数没有任何影响。

有什么想法在这里发生了什么?对于它的价值,有问题的命令是InstallShield 2012中的iscmdbld.exe。

我尝试过的其他命令(例如' dir')不会出现此问题。

只有在cmd窗口中运行命令时,才能查看程序的完整输出。

4 个答案:

答案 0 :(得分:2)

不幸的是,我没有安装InstallShield,所以我很难运行一些测试,但是我已经完成了在处理输入和输出时没有采取行动的程序。在正常情况下">"应该没有限制,我在后台运行ghostscript和其他旧dos程序的Windows服务器上使用它很多,并且将输出管道传输到文件的唯一方法是使用>,有时我有相当多Mb的文件,所以200行真的必须用当前的exe做一些事情。

我只能建议尝试一些解决方法,例如你可以尝试tee32,它是一个小的免费软件,它将所有dos屏幕输出捕获到一个文件。因此,您将在屏幕上看到输出,并且您也将它存入文件中。

您可以阅读更多相关信息:here很遗憾,页面上提到的链接不起作用,但我能够找到它的工作副本:here

我真的希望这会帮助你克服这个问题。

了Emil

答案 1 :(得分:1)

如果它正在写入另一个流,请尝试:

somecommand > results.txt 2>&1 3>&1 4>&1 5>&1 6>&1 7>&1 8>&1 9>&1

另一种可能性是该工具使用直接屏幕写入输出的一部分 - 在MSDOS时代,有屏幕阅读TSR程序可能会有所帮助。

答案 2 :(得分:1)

您的问题的另一种解决方案可能是构建一个捕获输出并将其发送到文件的C#程序。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CaptureCMDOutput
{
    class Program
    {
        static string _Filename = @"sc.exe";
        static string _Arguments = @"query";
        static string _outputfile = @"c:\debug.txt";

        static void Main(string[] args)
        {
            var processStartInfo = new ProcessStartInfo
            {
                FileName = _Filename, // Exe file to run
                Arguments = _Arguments, // Arguments to exe file
                RedirectStandardOutput = true,
                UseShellExecute = false
            };

            var process = Process.Start(processStartInfo);
            process.OutputDataReceived += process_OutputDataReceived;
            process.BeginOutputReadLine();
            process.WaitForExit();
            process.CancelOutputRead();

            Console.ReadKey();
        }

        static void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Console.WriteLine(e.Data);

            using (StreamWriter writer = new StreamWriter(_outputfile, true))
            {
                writer.WriteLine(e.Data);
            }
        }


    }
}

答案 3 :(得分:0)

另一个方法是附加('>>')而不是重新开始('>')。但是,我无法想到如何在不了解您的问题的情况下生成您所看到的内容。