Failed to redirect output message on Windows command line (cmd.exe)

I tried to run the following command on a windows command prompt.

abc.exe >log.txt 2>&1 

I expect all output from abc.exe be redirected to log.txt , but it does not work as log.txt empty.

However, if I just executed abc.exe , the output will appear on the Windows command prompt.

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

+8
source share
3 answers

Application: starting with Windows 10 v1809, Windows finally supports pseudo- consoles. If available, this offers a better solution than using the legacy console API.


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

CreateConsoleScreenBuffer and SetConsoleActiveScreenBuffer allow you to switch to a dedicated screen buffer so as not to interfere with the existing one.

SetConsoleScreenBufferSize can make the buffer wide enough to avoid SetConsoleScreenBufferSize strings.

SetConsoleCursorPosition can set the cursor position as required.

After you run the program, ReadConsoleOutput will allow 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 the extra buffer.

+1
source

The symptom is that console output is not displayed if redirection to a file can be caused by the absence of flush() in the program that writes to standard output. However, the output should be visible when the program exits (gracefully) or when the corresponding buffer is filled and automatically reset.

0
source

I see a problem ... first, your standard output writes to a file, and then gives an error, so if you had no errors, you get an empty file!

Instead, try adding error output to the same file, for example:

 abc.exe >log.txt 2>>&1 

Strike>

-one
source

All Articles