简体   繁体   中英

unable to redirect output message in windows command prompt (cmd.exe)

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM