9

I tried to run the following command in Windows command prompt.

abc.exe >log.txt 2>&1

I'm expecting all output from abc.exe to be directed to log.txt, but it doesn't work, as the log.txt is empty.

However, if I just execute abc.exe, the output is showing up in Windows command prompt.

I'm not sure what is the output handler used by this application (STDOUT or STDERR), but I'm wondering is there a way to capture all messages regardless of the handler.

6
  • perl abc.exe >log.txt 2>&1 try this
    – run
    Nov 3, 2011 at 6:08
  • When you say "it doesn't work" what do you mean? The program doesn't run at all? It runs, but there's no output? It runs, but the output goes to the console? Nov 3, 2011 at 19:54
  • Hi Johnston, it does generate the file, but it is empty. the output doesn't go to console
    – TimMe
    Nov 4, 2011 at 5:51
  • Hi Run, it doesn't work, and i don't understand the rational of your approach
    – TimMe
    Nov 4, 2011 at 5:54
  • 1
    It sounds as if a bug in the program is making it crash when the output is redirected. Have you contacted the vendor? Nov 5, 2011 at 4:18

2 Answers 2

1

Addendum: as of Windows 10 v1809, Windows finally supports pseudoconsoles. If available, this offers a better solution than using the legacy console API.


If you really need to capture that message, use the console API.

CreateConsoleScreenBuffer and SetConsoleActiveScreenBuffer allow you to switch to a dedicated screen buffer to avoid interfering with the existing one.

SetConsoleScreenBufferSize can make the buffer wide enough to avoid line rollover.

SetConsoleCursorPosition can set the cursor position as required.

After you've run the program, ReadConsoleOutput allows you to read what it wrote to the console.

You can then use GetStdHandle(STD_OUTPUT_HANDLE) and SetConsoleActiveScreenBuffer to return the console to the original buffer, and CloseHandle to close your extra buffer.

3
  • 1
    Thanks for the discussion guys. It turns out Jonston is right, the vender admitted it is a bug in this program, which causes message couldn't redirect correctly. however, it seems weird to me that the error can be printed out to the windows console, but no way for us to capture it programmatically.
    – TimMe
    Dec 17, 2011 at 14:42
  • 1
    "it's a feature, not a bug": (echo hello & echo world>con)>file.txt echoes world to the screen, and you are only able to "capture" hello to the file. This way you can ensure, the user can see critical messages on the screen, even when a code block or even the whole script is redirected.
    – Stephan
    Nov 8, 2018 at 15:09
  • @Stephan, not in this particular case: the program in question wasn't generating any output when output was redirected, not even to the console. The code probably assumed that standard output was a console handle (and made use of console-specific API calls) rather than opening a console handle themselves. Nov 8, 2018 at 18:36
0

The symptom that console output is not visible when redirected to a file can be due to a missing flush() in the program that writes to the standard output. However, the output should be visible when the program exits (gracefully) or when the respective buffer fills up and is flushed automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.