Home > Net >  Windows batch redirect output to console
Windows batch redirect output to console

Time:05-20

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

CodePudding user response:

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

  • Related