Windows批处理重定向输出到控制台

发布于 2025-01-29 13:21:35 字数 1975 浏览 5 评论 0 原文

这个问题的摘要是,我有一个命令,我正在批处理, some.exe args ,但它没有输出到控制台。但是,如果使用 some.exe args> test.txt 。我尝试了@some.exe args some.exe args&gt> con 将其输出到控制台,但似乎都没有起作用。

还有其他方法可能有效吗?


这就是从上一个问题中提出的,我问门使互动式窗口成为互动窗口

我正在通过批处理脚本调用名为门的程序。它运行一个简单的脚本, hello.dxl 看起来像

cout << "Hello world"

批处理脚本, run.bat 看起来像

"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl

运行时,屏幕上没有输出,没有输出弹出窗口或其他任何内容(如果 hello.dxl print(“ Hello world”)一个交互式窗口将弹出,但不要使用 cout

如果我添加&gt; test.txt 到命令的末尾,

"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > test.txt

它成功地输出了 Hello World test.txt 。我注意到的是,当使用 print(“ Hello world”)没有发送到 test.txt 文件和一个交互式窗口,因此它看起来像 cout 是必经之路。

因此,尽管我可能只是在任何地方都不会输出输出,因此我尝试添加&gt; con 试图强迫它进入控制台。

"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > CON

但这仍然导致空白输出。

我还尝试在命令之前添加@,如此 batch -redirect program输出到当前console console ,喜欢

@"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl

@"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > CON

没有运气,

我本来想在没有门的情况下重现这个问题,但我不知道是什么首先造成了它。


编辑:我并不是真的想使用&gt; test.txt&amp;键入test.txt 这是我正在使用的当前解决方法。但理想情况下,我不希望它输出到文本文件

The summary of this question is I have a command I'm running in batch, Some.exe args but it is not outputting to the console. It does however output to a text file if used like Some.exe args > test.txt. I've tried stuff like @Some.exe args and Some.exe args > CON to get it to output to the console, but neither seems to work.

Are there any other approaches which might work?


This follows on from a previous question I asked DOORS make console the interactive window.

I'm calling a program called DOORS through a batch script. It runs a simple script, hello.dxl that looks like

cout << "Hello world"

The batch script, Run.bat looks like

"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl

When this is run, no output appears on the screen and there are no popup windows or anything (if hello.dxl said print("Hello World") an interactive window would pop-up, but not with cout)

If I add > test.txt to the end of the command

"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > test.txt

It outputs the Hello World to test.txt successfully. Something I noticed is when using print("Hello World") there was no output sent to the test.txt file and an interactive window popped up so it looks like cout is the way to go.

So I though the output might just not be being output anywhere, so I tried adding > CON instead to try to force it to go to the console.

"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > CON

But that still resulted in a blank output.

I also tried adding an @, before the command, as suggested in this Batch - redirect program output to current console, like

@"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl

or

@"C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" -u test -pass testPass -b hello.dxl > CON

But no luck there either

I would have tried to reproduce this issue without DOORS but I don't know what is causing it in the first place.


Edit: I'm not really looking to use > test.txt & type test.txt as that is the current workaround I am using. But ideally I don't want it outputting to a text file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撧情箌佬 2025-02-05 13:21:35

事实证明,这种方法的功能有限,一旦我尝试打开一个模块,它就会使交互式窗口弹出,并且在交互式窗口或“控制台”窗口中看不到输出,

这不是&gt; test.txt&amp;键入test.txt


基于@pa。但是,如果它是重定向的,所以我编写了一个小C#控制台应用程序,它看起来像

using System;
using System.Diagnostics;

using Process process = new();
process.EnableRaisingEvents = true;
process.ErrorDataReceived += ErrOut;
process.OutputDataReceived += StdOut;
process.StartInfo.FileName = args[0].Trim();
process.StartInfo.Arguments = args[1].Trim();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();

static void ErrOut(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
    if (dataReceivedEventArgs.Data != null)
    {
        Console.WriteLine(dataReceivedEventArgs.Data);
    }
}

static void StdOut(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
    if (dataReceivedEventArgs.Data != null)
    {
        Console.WriteLine(dataReceivedEventArgs.Data);
    }
}

我然后使用命令将其导出到一个EXE中,

dotnet publish /p:DebugType=None /p:DebugSymbols=false /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishReadyToRunShowWarnings=true /p:PublishTrimmed=false /p:IncludeNativeLibrariesForSelfExtract=true /p:IncludeAllContentForSelfExtract=true "RedirectOutput.csproj" -o "." -c release

然后我添加了该命令,然后将其添加到我的路径环境变量中。

现在我可以做

RedirectOutput "C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" "-u test -pass testPass -b hello.dxl"

,我得到了所需的输出

Turns out this approach has limited functionality, as soon as I tried opening a module, it made the interactive window pop up and no output was visible in the interactive window or the the console window

This is not an issue with the > test.txt & type test.txt workaround


Based on @PA.'s suggestion that the program is somehow preventing output to stdout but not if it was redirected, so I wrote a small C# console app which looks like

using System;
using System.Diagnostics;

using Process process = new();
process.EnableRaisingEvents = true;
process.ErrorDataReceived += ErrOut;
process.OutputDataReceived += StdOut;
process.StartInfo.FileName = args[0].Trim();
process.StartInfo.Arguments = args[1].Trim();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();

static void ErrOut(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
    if (dataReceivedEventArgs.Data != null)
    {
        Console.WriteLine(dataReceivedEventArgs.Data);
    }
}

static void StdOut(object sender, DataReceivedEventArgs dataReceivedEventArgs)
{
    if (dataReceivedEventArgs.Data != null)
    {
        Console.WriteLine(dataReceivedEventArgs.Data);
    }
}

I then exported this to a single exe using the command

dotnet publish /p:DebugType=None /p:DebugSymbols=false /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:PublishReadyToRunShowWarnings=true /p:PublishTrimmed=false /p:IncludeNativeLibrariesForSelfExtract=true /p:IncludeAllContentForSelfExtract=true "RedirectOutput.csproj" -o "." -c release

which I added then just added the folder with that exe in to my path environment variable.

Now I can just do

RedirectOutput "C:\Program Files\IBM\Rational\DOORS\9.6\bin\doors.exe" "-u test -pass testPass -b hello.dxl"

and I get the desired output

人心善变 2025-02-05 13:21:35

这更多是解决方法。
这个想法是使用管道程序,以将Windows程序的输出重定向到控制台:

SomeWinProg SomeArgs 2>>&1 | SomePipeProg PipeProgArgs

作为管道程序,您可以使用通过所有内容传递所有内容的程序,例如:

SomeWinProg SomeArgs 2>>&1 | findstr /r "/c:.*"

是否有效取决于Windows程序。

关于时间:
当您长时间运行Windows程序时,可能会出现一些麻烦,该程序会产生零星的输出,或者始终完成输出始终到同一行(无线供稿,只有托架返回),并且您想实时实时输出。<<<<<<<<<<<<<<<<<<< br>
在这些cacses中,例如 findstr 查找有点弱。
在github上,实现了 mtee ,这更合适:

SomeWinProg SomeArgs 2>>&1 | mtee nul

This is more a workaround.
The idea is to a use a pipe program in order to redirect the output of the Windows program to the console:

SomeWinProg SomeArgs 2>>&1 | SomePipeProg PipeProgArgs

As a pipe program you may use a program that passes throug everything, like:

SomeWinProg SomeArgs 2>>&1 | findstr /r "/c:.*"

If this works or not depends on the Windows program.

Concerning the timing:
There may be some trouble when you have a long time running Windows program which produces sporadic output or when the output is done always to the same line (no line feeds, only carriage returns) and you want to have the output in real time.
In these cacses Windows programs like findstr or find are a little bit weak.
On github there is an implementation of mtee thats fits better:

SomeWinProg SomeArgs 2>>&1 | mtee nul
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文